Board index » delphi » Typecast SET to INTEGER ?

Typecast SET to INTEGER ?

I realize that this is not a true database question, but most of my work
is in database and I tend not to follow the other Delphi Groups.

Altho there must be a simple way to typecast an integer to a set and
visa versa, I can't seem to get it.  Read the docs, etc.  Have not had
this prob with other languages.  I can think of inelegant ways to do it,
but I much prefer simple elegance.

Any suggestions?

Thx,

Joe

--
==============================================
Joe Steponaitis         Phone: 902 826-7483
FENIX Software          Fax..: 902 826-1404
POB 48                  Email: fe...@atcon.com
Boutilier's Point,
Nova Scotia
CANADA  B0J 1G0         Waaas Sapeneeen???????
==============================================

 

Re:Typecast SET to INTEGER ?


In article <31DBFE83.7...@atcon.com>, fe...@atcon.com says...

Quote

>I realize that this is not a true database question, but most of my work
>is in database and I tend not to follow the other Delphi Groups.

>Altho there must be a simple way to typecast an integer to a set and
>visa versa, I can't seem to get it.  Read the docs, etc.  Have not had
>this prob with other languages.  I can think of inelegant ways to do it,
>but I much prefer simple elegance.

>Any suggestions?

>Thx,

>Joe

Hi Joe,

I have tried it and it should work:

procedure TForm2.HCButton1Click(Sender: TObject);
TYPE TXX = (A, B, C, D, E, F, G, H, I, J, K);
    TSetOfXX = Set Of TXX;
VAR
    AnySet : TSetOfXX;
    wSet : Word;
begin
  AnySet := [A, B, C, D, E, F, G, H, I];
  wSet := Word (AnySet);
end;

Hope it helps

Bye

Andreas

=============================================
helicon software development - Andreas Prucha
Baden/Vienna, Austria - heli...@helicon.co.at
=============================================

Re:Typecast SET to INTEGER ?


On Thu, 04 Jul 1996 14:25:23 -0300, Joe Steponaitis <fe...@atcon.com>
wrote:

Quote
>I realize that this is not a true database question, but most of my work
>is in database and I tend not to follow the other Delphi Groups.

>Altho there must be a simple way to typecast an integer to a set and
>visa versa, I can't seem to get it.  Read the docs, etc.  Have not had
>this prob with other languages.  I can think of inelegant ways to do it,
>but I much prefer simple elegance.

   IntValue := Byte(SetValue); { for a set with up to 8 elements }

Post the exact code that causes problems, so we can help you.
Otherwise, we can only guess...
--
Ray Lischner                              li...@tempest-sw.com
Tempest Software, Corvallis, Oregon, USA  http://www.tempest-sw.com

Re:Typecast SET to INTEGER ?


In article <4ri61l$...@newsfeed.via.at>,
heli...@helicon.co.at (Andreas Prucha / helicon) wrote:

Quote
>I have tried it and it should work:

>procedure TForm2.HCButton1Click(Sender: TObject);
>TYPE TXX = (A, B, C, D, E, F, G, H, I, J, K);
>    TSetOfXX = Set Of TXX;
>VAR
>    AnySet : TSetOfXX;
>    wSet : Word;
>begin
>  AnySet := [A, B, C, D, E, F, G, H, I];
>  wSet := Word (AnySet);
>end;

Close, but only half of the sigar.

A set can contain up to 256 values, so you'll need 32 bytes to hold the
hold shebang (sp?)...

Some experimental code:
{$APPTYPE CONSOLE}
var
  s:set of byte;
  a:array[0..31] of byte;
  i:integer;
begin
  s:=[4, 5, 133];
  for i:=0 to sizeof(s)-1 do
  begin
    a[i]:=byte(pointer(integer(@s)+i)^); //store the set into the array
    write(a[i], ' ');  //bit 4 and 5 is set in the first byte = 48
  end;
  //now the other way, from array to the set:
  s:=[];
  for i:=0 to sizeof(s)-1 do
  begin
    p:=pointer(integer(@s)+i);
    p^:=a[i];
  end;
end.

--
=\
 *=- R.Moberg, CD-Player Pro info @ http://www.sn.no/~mobergru/
=/

Other Threads