Solved

SETs in COM

Posted on 1998-07-30
10
141 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now