Forum > LCL

TStringList with objects

(1/15) > >>

mtanner:
Not sure if this is the right section of the forum.
I am using a TStringList, adding lines of text, and some lines have an object associated with them, with Add or AddObject. This works fine, and I can retrieve individual lines of text ok.

But I cannot figure out how to retrieve an object associated with a line. I'be looked at the code and all the documentation that I can find. Could someone please just respond with a single line of code that retrieves the object assocuated with a specifie line in the TStringList?

I have tried things like
mylist.objects;
mylist.object;
mylist.GetObject(I);
mylist.GetObject;

but all of these are apparently wrong. I've seen lots of stuff about using TStringList, intriductory to very complex, but cannot find the answer to the above seemingly trivial question.
FI

Zvoni:
??
Untested! https://www.freepascal.org/docs-html/rtl/classes/tstrings.objects.html

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Var   MyClass : TMyClass;  //Or TObjectBegin   MyClass:=TMyClass(MyList.Objects[SomeValidIndex]);  //If it's TObject above, no need for CastingEnd; 

Sieben:
The corresponding object of


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---mylist.Strings[i]
is


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---mylist.Objects[i]
It returns a TObject, so if you want to access the class you initially put there you have to use a typecast as Zvoni already mentioned. And as you wrote that only some lines are associated with an object, you'd better test like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if Assigned(myList.Objects[i]) then  // equivalent to: if (myList.Objects[i] <> nil) then  myclass := TMyClass(myList.Objects[i]);
or, even better:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (myList.Objects[i] is TMyClass) then  myclass := TMyClass(myList.Objects[i]);
The 'is'-test tests for nil as well. And you know for sure that it is actually of the class you want and expect.

mtanner:
Thank you.

Handoko:
I'm late. But I hope this demo can be helpful maybe for others:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    CheckBox1: TCheckBox;    Label1: TLabel;    Memo1: TMemo;    RadioButton1: TRadioButton;    procedure Button1Click(Sender: TObject);    procedure CheckBox1Click(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCreate(Sender: TObject);    procedure Memo1Change(Sender: TObject);    procedure RadioButton1Click(Sender: TObject);  private    Info: TStringList;    procedure SetLink;    function WhichLine(TheObject: TObject) : string;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Info := TStringList.Create;  SetLink;end; procedure TForm1.Memo1Change(Sender: TObject);begin  SetLink;end; procedure TForm1.RadioButton1Click(Sender: TObject);begin  ShowMessage(WhichLine(CheckBox1));end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  Info.Free;end; procedure TForm1.Button1Click(Sender: TObject);begin  ShowMessage(WhichLine(Button1));end; procedure TForm1.CheckBox1Click(Sender: TObject);begin  ShowMessage(WhichLine(CheckBox1)); // Sorry, does not work on GTK2end; procedure TForm1.SetLink;var  i: Integer;begin   Info.Clear;  Info.Assign(Memo1.Lines);   if Info.Count > 0 then    Info.Objects[0] := Button1;  if Info.Count > 1 then    Info.Objects[1] := CheckBox1;  if Info.Count > 2 then    Info.Objects[2] := RadioButton1;  if Info.Count > 3 then    Info.Objects[3] := Label1;   Button1.Hint      := '';  CheckBox1.Hint    := '';  RadioButton1.Hint := '';  Label1.Hint       := '';  for i := 0 to Info.Count-1 do    (Info.Objects[i] as TControl).Hint := Info[i]; end; function TForm1.WhichLine(TheObject: TObject): string;var  Index: Integer;  i:     Integer;begin  Index := -1;  for i := 0 to Info.Count-1 do    if Info.Objects[i] = TheObject then    begin      Index := i;      Break;    end;  if Index < 0 then Exit;  Result := 'Index of the line associated is = ' + Index.ToString;end; end.

Navigation

[0] Message Index

[#] Next page

Go to full version