The way you do it now is wonderful.
You cannot do this with a record without allocating memory when adding a new record to TStringList.Objects
, and you will have to free it later. You use the class just as well as you do now; you must free objects before freeing the string list. (In later versions of Delphi, the TStringList
has the OwnsObjects
property, which will automatically free them for you when the list of strings is free, but this is not in Delphi 7.)
If you really want to do this with a write, you can:
type PRec = ^TRec; TRec = record str: string; num: Integer; end; var rec: PRec; begin for i := 0 to 9 do begin System.New(Rec); rec.str := 'rec' + IntToStr(i); rec.num := i*2; Alist.AddObject(IntToStr(i), TObject(Rec)); // how to write here? end; end;
You need to use System.Dispose(PRec(AList.Objects[i]))
to free memory before freeing the string list. As I said, the way you do it now is actually a lot easier; you donβt need to do type when adding and removing from string list.
By the way, you do not need AList.Clear
. Since you are creating a string list, nothing can be deleted.
source share