Referring to objects in variables lazarus / 61-Beginners ---------------------------------------------------------------------- heebiejeebies · 2022-01-23 20:12 Feel like this is a dumb question, but can't find an answer anywhere.  How do I refer to an object name using a variable? I've been getting around this with routines like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i varWeather: Shortint;  beginif weather = 1 then Sunnyicon.visible :=true;if weather = 2 then Cloudyicon.visible :=true;  But I'm currently working on a complicated routine that will take me forever to program like that.  Is there a way to just put the name of the object into a variable and then refer to it directly?  So theoretically it would be something more like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i varWeather: object;  beginWeather.visible :=true;  Thanks! ---------------------------------------------------------------------- howardpc · 2022-01-23 23:31 For classes that descend from TComponent (such as TImage) you can use the FindComponent method. For example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TForm1 = class(TForm)    Image1: TImage;    procedure FormCreate(Sender: TObject);  public    sunny, cloudy: TImage;    procedure CreateImages;    function IconVisibleSetOK(const aImageName: String; isVisable: Boolean): Boolean;    procedure ShowSunWithNoClouds;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.CreateImages;begin  cloudy := TImage.Create(Self);  cloudy.Name := 'cloudy';  //cloudy.Picture.Icon.LoadFromFile(...);  sunny := TImage.Create(Self);  sunny.Name := 'sunny';  //sunny.Picture.Icon.LoadFromFile(...);end; procedure TForm1.FormCreate(Sender: TObject);begin  CreateImages;  ShowSunWithNoClouds;end; function TForm1.IconVisibleSetOK(const aImageName: String; isVisable: Boolean): Boolean;var  cmp: TComponent;begin  cmp := FindComponent(aImageName);  Result := Assigned(cmp) and (cmp is TImage);  if Result then    TImage(cmp).Visible := isVisible;end; procedure TForm1.ShowSunWithNoClouds;begin  Assert(IconVisibleSetOK('sunny', True), 'missing sunny icon!');  Assert(IconVisibleSetOK('cloudy', False), 'missing cloudy icon!');end; ---------------------------------------------------------------------- heebiejeebies · 2022-01-24 19:39 Thanks guys! It seems that using a TComponent variable is the easiest way, especially since I've already created a lot of my components. Now, how does one clear a TComponent variable?  Normally I just set an int to 0 or set a string to two apostrophes.  The TComponent variables are having none of that! Also, I should point out that my code is much simpler than Howardpc's code above.  I've simply gone with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  var ObjectAVar : TComponent;begin  TImage(ObjectAVar).Visible :=true; I haven't bothered with FindComponent or anything and the code works.  Am I exposing myself to some danger by doing it this simple way? ---------------------------------------------------------------------- howardpc · 2022-01-24 21:15 Quote from: heebiejeebies on January 24, 2022, 08:39:33 pmI haven't bothered with FindComponent or anything and the code works.  Am I exposing myself to some danger by doing it this simple way? You only show code snippets so others cannot assess the safety or otherwise of your approach. But the unprotected cast of ObjectAVar to a TImage looks risky to me. Why is the cast needed? What is ObjectAVar? It is not clear what you are asking by "clearing" a TComponent variable. If you are talking about a TImage or TImage descendant, do you mean TImageInstance.Free, or TImageInstance.Graphic.Clear? ---------------------------------------------------------------------- heebiejeebies · 2022-01-25 01:25 Sorry even though I've been at this a while I'm still a bit of a n00b - I don't really understand what you mean by unprotected casts.  Anyway, I should have explained more clearly.  I have a number of screens that the user can click through, and I am trying to create a back button here.  Each screen has a series of varying objects like text fields, list boxes and various image choices.  The user chooses which image is visible by clicking on various buttons.  Only one image is visible at a time.  The different screens can be reused for various purposes, so I need to store all the user-changeable data on each screen, so that it can be restored back to its former state when the user clicks the back button.  I have various string/int arrays that store the data in the other fields, and that all seems to work fine.  The question here is purely about storing which image is visible. ObjectAVar is a global variable of TComponent that saves the name of the currently visible image.  There is also a corresponding array of TComponent, called ObjectA, so when the user changes screen, a new line gets created in the TComponent array and ObjectAVar is saved there. So, the code on the user screen for selecting Image1 will be something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin// code here to hide all other images, then ... Image1.Visible :=true;ObjectAVar :=Image1;end  It gets put into the ObjectA array when the user goes to a new screen. Then in the back button, when we return to this screen, we'll fetch the correct visible image from the ObjectA array, then show that image: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin// code here to hide all other images, then ...   ObjectAVar := ObjectA[High(ObjectA)];  TImage(ObjectAVar).Visible :=true; Having tried it, this works. So I'm just a bit confused what all the other code in your example was about, and whether I've missed something crucial by making mine so simple.  Thanks ! ---------------------------------------------------------------------- heebiejeebies · 2022-01-26 23:24 I guess I'm just going to keep doing it this way. I can't see any dangers with it. Thanks for your help guys! ---------------------------------------------------------------------- heebiejeebies · 2022-01-27 21:24 Good point, that could have been embarrassing.  Thanks