Solved

SETs in COM

Posted on 1998-07-30
10
138 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

20 Experts available now in Live!

Get 1:1 Help Now