Solved

SETs in COM

Posted on 1998-07-30
10
152 Views
Last Modified: 2010-04-04
In an OLE automation object, I'd like to include a property that
behaves like a set. But how do I do this? I guess an interfaced enum,
but code like this won't compile:

    options:=[RASEO_TerminalBeforeDial, RASEO_IpHeaderCompression];


options being an interfaced enum, with both values in the "set" actual
values that belong to the enum.

How do I get this to work?
0
Comment
Question by:lowlevel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 4

Expert Comment

by:d003303
ID: 1359127
You have to differ between a set and an enumerated type. A set can contain one or more elements, an enumerated type only one.
Normally, you will have to provide an enumeration procedure that steps through the members of the set.
For enumerated types, use the type TOLEEnum to be automation compatible.

Slash/d003303
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1359128
This is the equivalent to a set...check out Windows.pas...This is how Windows does sets...

IDX_Prop1 = 1;  //$00000001   000000001
IDX_Prop2 = 2;  //$00000002   000000010
IDX_Prop3 = 4;  //$00000004   000000100
IDX_Prop4 = 8;  //$00000008   000001000
IDX_Prop5 = 16; //$00000010   000010000
IDX_Prop6 = 32; //$00000020   000100000
IDX_Prop7 = 64; //$00000040   001000000
IDX_Prop8 = 128;//$00000080   010000000
IDX_Prop9 = 256;//$00000100;  100000000

.

Just change everything to a constant...

Rick




0
 
LVL 3

Expert Comment

by:rickpet
ID: 1359129
Here's an example...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
const
IDX_Prop1 = 1;  //$00000001   000000001
IDX_Prop2 = 2;  //$00000002   000000010
IDX_Prop3 = 4;  //$00000004   000000100
IDX_Prop4 = 8;  //$00000008   000001000
IDX_Prop5 = 16; //$00000010   000010000
IDX_Prop6 = 32; //$00000020   000100000
IDX_Prop7 = 64; //$00000040   001000000
IDX_Prop8 = 128;//$00000080   010000000
IDX_Prop9 = 256;//$00000100;  100000000

type
  TEnumProp = (Prop1, Prop2, Prop3, Prop4, Prop5, Prop6, Prop7, Prop8, Prop9);
  TEnumPropSet = set of TEnumProp;
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FEnumPropSet: TEnumPropSet;
  protected
    function GetEnumProp: integer;
  public
    { Public declarations }
    property EnumPropSet: TEnumPropSet read FEnumPropSet write FEnumPropSet;
    property EnumPropValue: integer read GetEnumProp;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.GetEnumProp: integer;
begin
  result := 0;
  if Prop1 in EnumPropSet then
    Result := IDX_Prop1;
  if Prop2 in EnumPropSet then
    Result := Result + IDX_Prop2;
  if Prop3 in EnumPropSet then
    Result := Result + IDX_Prop3;
  if Prop4 in EnumPropSet then
    Result := Result + IDX_Prop4;
  if Prop5 in EnumPropSet then
    Result := Result + IDX_Prop5;
  if Prop6 in EnumPropSet then
    Result := Result + IDX_Prop6;
  if Prop7 in EnumPropSet then
    Result := Result + IDX_Prop8;
  if Prop8 in EnumPropSet then
    Result := Result + IDX_Prop8;
  if Prop9 in EnumPropSet then
    Result := Result + IDX_Prop9;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  EnumPropSet := [Prop1, Prop4, Prop5];
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := IntToStr(EnumPropValue);
end;

end.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:rickpet
ID: 1359130
opps... forgot the set value...

property EnumPropValue: integer read GetEnumProp write SetEnumProp;

procedure TForm1.SetEnumProp(value: integer);
begin
  FEnumPropSet := [];
  if (Value and IDX_Prop1)=IDX_Prop1  then
    FEnumPropSet:= FEnumPropSet+ [Prop1];
  if Value and IDX_Prop2= IDX_Prop2 then
    FEnumPropSet:= FEnumPropSet+ [Prop2];
  if Value and IDX_Prop3= IDX_Prop3 then
    FEnumPropSet:= FEnumPropSet+ [Prop3];
  if Value and IDX_Prop4= IDX_Prop4 then
    FEnumPropSet:= FEnumPropSet+ [Prop4];
  if Value and IDX_Prop5= IDX_Prop5 then
    FEnumPropSet:= FEnumPropSet+ [Prop5];
  if Value and IDX_Prop6= IDX_Prop6 then
    FEnumPropSet:= FEnumPropSet+ [Prop6];
  if Value and IDX_Prop7= IDX_Prop7 then
    FEnumPropSet:= FEnumPropSet+ [Prop7];
  if Value and IDX_Prop8= IDX_Prop8 then
    FEnumPropSet:= FEnumPropSet+ [Prop8];
  if Value and IDX_Prop9= IDX_Prop9 then
    FEnumPropSet:= FEnumPropSet+ [Prop9];
end;



0
 
LVL 3

Accepted Solution

by:
rickpet earned 150 total points
ID: 1359131
Note...you would get rid of the interface to the set as you can not use this in COM and just have and interface to an interger property.

I would move this to Protected/Private so that your code can use the sets and you don't have to modify your code much... =)

Your user would then use your new new set like this...

EnumPropValue := IDX_Prop2 + IDX_Prop4;

Rick


0
 
LVL 3

Expert Comment

by:rickpet
ID: 1359132
LowLevel...

Have you tried this out???Any problems???Any questions?

Rick
0
 
LVL 1

Author Comment

by:lowlevel
ID: 1359133
I have looked into it, but need more time (very busy here lately)..
I'll try to get it sorted this week.
0
 
LVL 1

Author Comment

by:lowlevel
ID: 1359134
checked it, it works :)

still sucks that it isn't supported by COM, and that there isn't a language feature for it (xEnumValue in xEnum)..

but at least this works :)

0
 
LVL 3

Expert Comment

by:rickpet
ID: 1359135
LowLevel...

aww...but there is...

  if xEnumValue and xEnum(xSet) then

works the same
as

  if xEnumValue in xEnum(xSet) then


if you AND a value against another value it will return true if the value is part of the value...

example

if true and true then  //true
bit pattern:
    01  and  01   =      01

if true and false then //false
bit pattern:
    01  and  00   =      00

if enum2+enum4 in [1,2,4]  then//true
bit pattern:

  001010 and 001011 = true (001010)

So you see Sets are just a more intuitive process when doing Bitwise manipulation...

Rick
0
 
LVL 1

Author Comment

by:lowlevel
ID: 1359136
oh, i knew all that :)

it's just that I'd like a "nice" language feature that allows me to evaluate "if enumvalue in enumtype then blah" as a boolean.
but i guess i can write a function around that...


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question