Home > Back-end >  How to shorten repetitive code regarding lable caption?
How to shorten repetitive code regarding lable caption?

Time:10-09

I have such a code

  Label1.Caption := '';
  Label2.Caption := '';
  Label3.Caption := '';
  Label4.Caption := '';
  Label5.Caption := '';
  Label6.Caption := '';

How can I make a loop or sth to make it shorter?

CodePudding user response:

Make simple procedure to work on array of labels:

procedure ClearLabels(LabelsArr: array of TLabel);
var
  i: Integer;
begin
   for i := Low(LabelsArr) to High(LabelsArr) do
      LabelsArr[i].Caption := '';
end;

and call it like that:

ClearLabels([Label1, Label2, Label3]);
  • Related