Handoko & Thaddy,
Handokos sample showed me the way, thanks!
I don't mean to waste your time, but trying to evolve from traditional Pascal to Object Pascal some background infos would be greatly appreciated.
This is Handokos routine, which works:
procedure TForm1.Button1Click(Sender: TObject);
var
MyList: TMyList;
S: string;
begin
S := 'one;two;three';
MyList := TMyList.Create(S); // 1
Memo1.Lines.AddStrings(MyList); // 2
MyList.Free; // 3
end;
What I did try to avoid was the lines 1 and 3, since I never actually intend to do something with MyList.
Memo2.Lines.AddStrings(MyList.Create(S)); // 4
Memo2.Lines.AddStrings(TMyList.Create(S)); // 5
OK, seems I cannot use (4), guess that's just because of the way the creators of Object Pascal decided their language should work, but why doesn't the compiler complain?
Line (5) works, but does cause a memory leak, since MyList.Free is never called. I considered this, but from what I was used to, variables declared as var and not beeing pointers with memory allocated manually (New/Dispose/GetMem/FreeMem) are automatically freed when their scope is exited. I did somehow expect that the same should happen to var MyList - the destructor should be called automatically. Why wasn't it?
I did also expect that this would eventually/likely render the Memo's list invalid, and thus tried to use Memo2.Lines.Assign, since what I read from the docs this routine doesn't just assign memory addresses but make an attribute-by-attribute copy of the original strings. Seems not to be the case, is it?
Please show me - for learnings sake - where I was wrong, and how one could have known. I understand Handokos code, but I don't understand why it was necessary to write it that way, whether there are other ways, especially whether it was possible to write code the way I had in mind ( a one statement "fire and forget" StringList)
Thnx,
Armin.