Variable as Identifier ... is it possible?? lazarus / 9-Editor ---------------------------------------------------------------------- Treco · 2009-01-13 03:47 Hi all, Because I'm new to pascal and Lazarus; There is any way to do something like this: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var   i: integer; begin   for i:=1 to 10 do     'Label+i'.Caption:='hello'; end; Do you see what I mean? where was to be the 'Label1.Caption:='hello';' I want to know if there is a way to loop from Label1 to Label10... or other way to do it. Thank you. Sorry for my very bad english. ---------------------------------------------------------------------- Leledumbo · 2009-01-13 05:16 QuoteI want to know if there is a way to loop from Label1 to Label10Use arrays, unfortunately you won't be able to use it along with the form designer. That means you have to create the labels yourself. You can do it in the OnCreate event of the main form like this: Code: [Select]type   TForm1 = class   ...   Labels: array of TLabel;   ...   end; ... procedure TForm1.Form1Create(Sender: TObject); var   i: Integer; begin   SetLength(Labels,10);   for i:=Low(Labels) to High(Labels) do begin     Labels[i]:=TLabel.Create(Self);     ... // set other Labels[i] properties   end; end; And destroy it when it's no longer needed: Code: [Select]procedure TForm1.Form1Destroy(Sender: TObject); var   i: Integer; begin   for i:=Low(Labels) to High(Labels) do     Labels[i].Free; end; ---------------------------------------------------------------------- Vincent Snijders · 2009-01-13 06:52 If you make the form the owner if the labels with TLabel.Create(Self), then the Labels are destroyed when you destroy the form, so adding the Labels.Free in the Form's OnDestroy event is not necessary. ---------------------------------------------------------------------- Leledumbo · 2009-01-13 07:34 Ah... yes, I forgot that every TComponent descendants will be freed when the parent is freed. ---------------------------------------------------------------------- Vincent Snijders · 2009-01-13 08:30 Not the parent, but the owner. Parent is the one who is responsible for the visible window, owner is the one who is responsible for the memory. Don't confuse them. ---------------------------------------------------------------------- Martin_fr · 2009-01-13 20:12 From Memory (I havent tested): for i := 1 to 10 do    (Form1.Findcomponent('label' + IntToStr(i))  as  TLabel).Caption := '---'; You may want to check that the result of Findcomponent <> nil, and that is "is" indeed a TLabel ---------------------------------------------------------------------- duncanparsons · 2009-01-14 14:41 Quote from: Vincent Snijders on January 13, 2009, 07:52:33 amIf you make the form the owner if the labels with TLabel.Create(Self), then the Labels are destroyed when you destroy the form, so adding the Labels.Free in the Form's OnDestroy event is not necessary. True. Either way, you want to call SetLength(Labels,0); to ensure that you act responsibly The form might be created and disposed on the fly, and you'd get a memory leak without that.. DSP ---------------------------------------------------------------------- Vincent Snijders · 2009-01-14 14:47 Why? Dynamic arrays (but not their contents, the object they contain) are automatically freed, when the last reference gets out of scope.