Search code examples
variablesdelphipascalnaming

Adressing multiple, similar variables/objects in Delphi


I am writing a program that uses many shapes and I need to create a procedure to turn them all white. The shapes in question are named SectorBorder1 to SectorBorder20.

Is there a way to adress the shapes like this or similarly?

SectorBorder[X].brush.color := ClWhite;
Inc(X);

...where X is the number (obviously), instead of having to do:

SectorBorder1.brush.color := ClWhite;
SectorBorder2.brush.color := ClWhite;
...
SectorBorder20.brush.color := ClWhite;

So basically being able to differentiate names through a variable. This is the only way I could think of describing it. (Sorry, could someone also maybe include a better discription?) Any advice would be greatly apreciated.


Solution

  • Use an array

    private
      SectorBorders: array[1..20] of TShape;
    
    procedure TMyForm.FormCreate(Sender: TObject):
    begin
      SectorBorders[1] := SectorBorder1;
      ..
      SectorBorders[20] := SectorBorder20;
    end;
    
    procedure TMyForm.SetAllToWhite;
    var
      X: Integer;
    begin
      for X := Low(SectorBorders) to High(SectorBorders) do
        SectorBorders[X].Brush.Color := clWhite;
    end;