flexterful
Programmer
Hi all.
I'm new to this forum so if you may excuse me for my mistakes.
I have written some lines of code :
// Object to store a sting in TStrings.Objects
TString = class(TObject)
private
fStr: String;
public
constructor Create(const AStr: String) ;
property Str: String read FStr write FStr;
end;
// </TString>
var
Form1: TForm1; // My form, contains Memo, ListBox and 2 Buttons
implementation
//-----------------------------------------------------------
// string object constructor
constructor TString.Create(const AStr: String) ;
begin
inherited Create;
FStr := AStr;
end;
//-----------------------------------------------------------
// method populates TString object passed via parameter with a
// string as a caption and a string as a object (via TString)
procedure PopulateStrings(Strings : TStrings);
const
S = 'asd';
begin
Strings.Clear;
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
end;
//-----------------------------------------------------------
// this one shows data, contained in Strings.Objects
// if none - prints 'nil'
procedure ShowStrings(Strings : TStrings);
var
i : Integer;
S : String;
begin
S := '';
for i := 0 to Strings.Count - 1 do
if Assigned(Strings.Objects)
then S := S + TString(Strings.Objects).Str + #13#10
else S := S + 'nil'#13#10;
ShowMessage(S);
end;
//-----------------------------------------------------------
// perform both mentioned methods for TMemo.Lines
procedure TForm1.Button1Click(Sender: TObject);
begin
PopulateStrings(Memo1.Lines);
ShowStrings(Memo1.Lines);
end;
//-----------------------------------------------------------
// perform both mentioned methods for TListBox.Items
procedure TForm1.Button2Click(Sender: TObject);
begin
PopulateStrings(ListBox1.Items);
ShowStrings(ListBox1.Items);
end;
//-----------------------------------------------------------
Result for TMemo.Lines (Button1.Click) :
---------------------------
Project1
---------------------------
nil
nil
nil
nil
---------------------------
OK
---------------------------
Result for TListBox.Items (Button2.Click) :
---------------------------
Project1
---------------------------
asd
asd
asd
asd
---------------------------
OK
---------------------------
Tested on Delphi5, Delpho7 and DelphiXE.
Why and where does TMemo (who uses same TStrings class for Lines as TListBox for his Items does) vanish all objects? Any ideas?
Thanks in advance
I'm new to this forum so if you may excuse me for my mistakes.
I have written some lines of code :
// Object to store a sting in TStrings.Objects
TString = class(TObject)
private
fStr: String;
public
constructor Create(const AStr: String) ;
property Str: String read FStr write FStr;
end;
// </TString>
var
Form1: TForm1; // My form, contains Memo, ListBox and 2 Buttons
implementation
//-----------------------------------------------------------
// string object constructor
constructor TString.Create(const AStr: String) ;
begin
inherited Create;
FStr := AStr;
end;
//-----------------------------------------------------------
// method populates TString object passed via parameter with a
// string as a caption and a string as a object (via TString)
procedure PopulateStrings(Strings : TStrings);
const
S = 'asd';
begin
Strings.Clear;
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
Strings.AddObject(S, TString.Create(S));
end;
//-----------------------------------------------------------
// this one shows data, contained in Strings.Objects
// if none - prints 'nil'
procedure ShowStrings(Strings : TStrings);
var
i : Integer;
S : String;
begin
S := '';
for i := 0 to Strings.Count - 1 do
if Assigned(Strings.Objects)
then S := S + TString(Strings.Objects).Str + #13#10
else S := S + 'nil'#13#10;
ShowMessage(S);
end;
//-----------------------------------------------------------
// perform both mentioned methods for TMemo.Lines
procedure TForm1.Button1Click(Sender: TObject);
begin
PopulateStrings(Memo1.Lines);
ShowStrings(Memo1.Lines);
end;
//-----------------------------------------------------------
// perform both mentioned methods for TListBox.Items
procedure TForm1.Button2Click(Sender: TObject);
begin
PopulateStrings(ListBox1.Items);
ShowStrings(ListBox1.Items);
end;
//-----------------------------------------------------------
Result for TMemo.Lines (Button1.Click) :
---------------------------
Project1
---------------------------
nil
nil
nil
nil
---------------------------
OK
---------------------------
Result for TListBox.Items (Button2.Click) :
---------------------------
Project1
---------------------------
asd
asd
asd
asd
---------------------------
OK
---------------------------
Tested on Delphi5, Delpho7 and DelphiXE.
Why and where does TMemo (who uses same TStrings class for Lines as TListBox for his Items does) vanish all objects? Any ideas?
Thanks in advance