Search code examples
delphirecordtstringlist

Delphi Stringlist in Record


Is it possible to have a stringlist in a record? EG

TImportStats = record
  ATotal:Integer;
  BTotal:String;
  AList:TStringist;
end;

and if I presume I would need to create it before using the record?


Solution

  • Whilst this is perfectly legal, it may be prudent to find another way. You pinpoint the issue when you said:

    I presume I would need to create it before using the record

    Not only that, but you need to find a good time to destroy it too. If you forget to do so there will be no errors but your program will leak memory.

    If the record is the owner of the string list then you may be better off containing it inside a class. That way the construction and destruction of the string list will follow the constructor/destructor pattern that all Delphi developers are familiar with.

    If the record does not own the string list, but just takes a reference to it during the lifetime of the string list, then a record is fine. But if you do it this way make sure that the record's lifetime is contained within the string list's lifetime so that you don't carry around a stale reference.