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

前端 未结 3 440
醉话见心
醉话见心 2021-02-06 12:29

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 enumerat

3条回答
  •  忘了有多久
    2021-02-06 12:37

    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;
    

提交回复
热议问题