Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 335
colin Avatar asked Sep 29 '26 06:09

colin


1 Answers

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.

like image 157
David Heffernan Avatar answered Oct 01 '26 03:10

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!