Delphi TList of records

后端 未结 8 1017
遥遥无期
遥遥无期 2020-12-13 02:55

I need to store a temporary list of records and was thinking that a TList would be a good way to do this? However I am unsure how to do this with a TList<

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 03:13

    We've just run into a similar issue here with a generic list of records. Hope the following psuedo code helps.

    type
      PPat = ^TPat;
      TPat = record
        data: integer;
      end;
    
    ...
    var
        AList: TList;
    
    ...
    procedure TForm1.Button1Click(Sender: TObject);
    var
      obj: PPat;
    begin
      obj := AList[0];
      obj.data := 1;
      Assert(obj.data = AList[0].data);  // correct
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      obj: PPat;
    begin
      AList := TList.Create;
      GetMem(obj, SizeOf(TPat));  // not shown but need to FreeMem when items are removed from the list
      obj.data := 2;
      AList.Add(obj);
    end;
    

提交回复
热议问题