The title pretty much says it all. I'm using a TClientDataset to store an array of objects, and one of the objects has a member defined as a set of an enumerated type. As I understand it, Delphi sets are bitfields whose size can vary from 1 to 32 bytes depending on how much data they contain, and Delphi doesn't define a TSetField. What sort of field should I use to load this value into?
Asked
Active
Viewed 3,946 times
7
-
See also [How to save/load Set of Types?](http://stackoverflow.com/q/9553510/757830). – NGLN Dec 25 '15 at 20:27
3 Answers
14
You could use a TBytesField or a TBlobField
ClientDataSet1MySet: TBytesField, Size=32
var
MySet: set of Byte;
Bytes: array of Byte;
begin
MySet := [1, 2, 4, 8, 16];
// Write
Assert(ClientDataSet1MySet.DataSize >= SizeOf(MySet), 'Data field is too small');
SetLength(Bytes, ClientDataSet1MySet.DataSize);
Move(MySet, Bytes[0], SizeOf(MySet));
ClientDataSet1.Edit;
ClientDataSet1MySet.SetData(@Bytes[0]);
ClientDataSet1.Post;
// Read
SetLength(Bytes, ClientDataSet1MySet.DataSize);
if ClientDataSet1MySet.GetData(@Bytes[0]) then
Move(Bytes[0], MySet, SizeOf(MySet))
else
MySet := []; // NULL
end;

Andreas Hausladen
- 8,081
- 1
- 41
- 44
-
1) To make your sample even clearer I propose to change the "ClientDataSet1MySet.SetData(@Bytes[0])" into "ClientDataSet1MySet.AsBytes := Bytes" 2) In the // Read block (on the Move line), I think that you must change the "SizeOf(MySet)" to "ClientDataSet1MySet.DataSize" to be consistent. – Roeland Van Heddegem Dec 04 '13 at 16:55
2
You can convert them to Byte, like this:
var
States : TUpdateStatusSet; // Can be any set, I took this one from DB.pas unit
SetAsAInteger: Integer;
dbs: Pointer; // Here's the trick
begin
States := [usModified, usInserted]; // Putting some content in that set
dbs := @States;
SetAsAInteger := PByte(dbs)^;
//Once you got it, SetAsAInteger is just another ordinary integer variable.
//Use it the way you like.
end;
To recover from anywhere:
var
MSG: string;
Inserted, Modified: string;
States: TUpdateStatusSet;
MySet: Byte;
begin
while not ClientDataSet.Eof do
begin
//That's the part that interest us
//Convert that integer you stored in the database or whatever
//place to a Byte and, in the sequence, to your set type.
iSet := Byte(ClientDatasetMyIntegerField);// Sets are one byte, so they
// fit on a byte variable
States := TUpdateStatusSet(iSet);
//Conversion finished, below is just interface stuff
if usInserted in States then
Inserted := 'Yes';
if usModified in States then
Modified := 'Yes';
MSG := Format('Register Num: %d. Inserted: %s. Modified:%s',
[ClientDataSet.RecNo, Inserted, Alterted]);
ShowMessage( MSG );
ClientDataset.Next;
end;
end;

Fabricio Araujo
- 3,810
- 3
- 28
- 43
-
That works just fine until you need more than 32 bits. Sets go up to 256 bits, and I'm in a situation where I might need a good fraction of that space. – Mason Wheeler Dec 09 '08 at 19:06
-
0
Based on the example of Andreas, but made somewhat simpler and clearer IMHO.
Tested on XE2
You could use a TBytesField or a TBlobField
ClientDataSet1MySet: TBytesField, Size=32
1) Writing
var
MySet: set of Byte;
Bytes: TBytes;
begin
MySet := [0];
// Write
Assert(ClientDataSet1Test.DataSize >= SizeOf(MySet), 'Data field is too small');
SetLength(Bytes, ClientDataSet1Test.DataSize);
Move(MySet, Bytes[0], SizeOf(MySet));
ClientDataSet1.Edit;
ClientDataSet1Test.AsBytes := Bytes;
ClientDataSet1.Post;
end;
2) Reading
var
MyResultSet: set of Byte;
begin
Move(ClientDataSet1Test.AsBytes[0], MyResultSet, ClientDataSet1Test.DataSize);
end;

Roeland Van Heddegem
- 1,623
- 2
- 20
- 35