0
pos := IntToStr(comp1[5]);
  with TLabel (FindComponent('Labelc'+pos)) do Caption:=comp1n;
  with TLabel (FindComponent('Labelc'+pos+'w')) do Caption:=IntToStr(comp1[1]);
  with TLabel (FindComponent('Labelc'+pos+'l')) do Caption:=IntToStr(comp1[2]);
  with TLabel (FindComponent('Labelc'+pos+'d')) do Caption:=IntToStr(comp1[3]);
  with TLabel (FindComponent('Labelc'+pos+'s')) do Caption:=IntToStr(comp1[4]);
pos := IntToStr(comp2[5]);
  with TLabel (FindComponent('Labelc'+pos)) do Caption:=comp2n;
  with TLabel (FindComponent('Labelc'+pos+'w')) do Caption:=IntToStr(comp2[1]);
  with TLabel (FindComponent('Labelc'+pos+'l')) do Caption:=IntToStr(comp2[2]);
  with TLabel (FindComponent('Labelc'+pos+'d')) do Caption:=IntToStr(comp2[3]);
  with TLabel (FindComponent('Labelc'+pos+'s')) do Caption:=IntToStr(comp2[4]);
pos := IntToStr(comp3[5]);
  with TLabel (FindComponent('Labelc'+pos)) do Caption:=comp3n;
  with TLabel (FindComponent('Labelc'+pos+'w')) do Caption:=IntToStr(comp3[1]);
  with TLabel (FindComponent('Labelc'+pos+'l')) do Caption:=IntToStr(comp3[2]);
  with TLabel (FindComponent('Labelc'+pos+'d')) do Caption:=IntToStr(comp3[3]);
  with TLabel (FindComponent('Labelc'+pos+'s')) do Caption:=IntToStr(comp3[4]);
...

and so on to the 10th. i was looking for something like FindComponent function but for the variables, so i can put one piece of code into for loop, but it seems like in pascal you cant concatenate string into variable names. all i need is to somehow dynamically change variable names so there is no need to write this code 10 times with the only thing changing is comp1,comp2,comp3,... comp1n, comp2n, comp3n, and so on...

any tips please? i would really appreciate! (sorry for bad english)

1ayre0
  • 1
  • 1
    Almost always, it is a bad idea to refer to components by their string names at runtime. Don't do that. Instead, use arrays or lists. See https://stackoverflow.com/questions/61710350/update-corresponding-label-depending-on-which-combobox-fired-the-event, https://stackoverflow.com/questions/18812960/use-variables-for-object-name-in-delphi and many more for examples. – Andreas Rejbrand Sep 13 '20 at 09:37
  • can you restructure `comp1, comp2, comp3` into `comp[1], comp[2], comp[3]`, and `comp1n, comp2n, comp3n` into `compn[1], compn[2], compn[3]`? then you can just iterate `comp` and `compn`. – iAmOren Sep 13 '20 at 11:27

1 Answers1

0

Untested, errorhandling is horrible, but at least the same as in the original.

const letters : array [1..4] of char =('w','l','d','s');

 Procedure TForm2.AssignLabel(pos:integer;const compxn:string;othercomp:array of integer);

 Var i : integer;
     poss : string;
 Begin
    poss:=inttostr(pos);
    TLabel(FindComponent('Labelc'+poss)).caption:=compxn;
    For i:=1 to 4 do
      TLabel(FindComponent('Labelc'+poss+letters[i])).Caption:=IntToStr(othercomp[i-1]);
    // othercomp i-1 because open array is always 0 based.
 end;

 // and then the calling reduces to   

 assignlabels(comp1[5],comp1n,comp1);
 assignlabels(comp2[5],comp2n,comp2);
 assignlabels(comp3[5],comp3n,comp3);

If pos is always the 5th value of the array, then

 Procedure TForm2.AssignLabel(const compxn:string;othercomp:array of integer);

 Var i : integer;
     poss : string;
 Begin
    poss:=inttostr(othercomp[5-1]);  // othercomp i-1 see below
    TLabel(FindComponent('Labelc'+poss)).caption:=compxn;
    For i:=1 to 4 do
      TLabel(FindComponent('Labelc'+poss+letters[i])).Caption:=IntToStr(othercomp[i-1]);
    // othercomp i-1 because open array is always 0 based.
 end;
Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89