Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to store a Delphi set in a dataset?

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?

like image 277
Mason Wheeler Avatar asked Dec 07 '08 13:12

Mason Wheeler


1 Answers

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;
like image 64
Andreas Hausladen Avatar answered Sep 30 '22 09:09

Andreas Hausladen