简体   繁体   中英

how can use dynamic array in delphi

i use dynamic array in delphi.

var

var
  frame3:array[0..10] of TFrame3
procedure TForm1.Button1Click(sender:TObject);
begin
    frame3[count] := TFrame3.create(self);
    gridpanel2.insertcontrol(frame3[count]);

but this code is 'A component named Frame3 alredy exists.' this error what can ido?

If you need multible instances of TFrame3 you need to give it a new name after you created it.

so change

 frame3[count] := TFrame3.create(self);
 gridpanel2.insertcontrol(frame3[count]);

To

 frame3[count] := TFrame3.create(self);
 frame3[count].Name := 'Frame3_' + InttoStr(Count);
 gridpanel2.insertcontrol(frame3[count]);

The other issue is that it is not seen in your code how you are changing count loop variable. Is it defined in advance?

You need to do something like this:

procedure TForm1.Button1Click(sender:TObject);
var count:byte;
begin
  for count:=1 to 10 do
    begin
      frame3[count] := TFrame3.create(self);
      ...
    end;

or use any other way to set count before each array member (class instance) creation. This code will even probably not require to set Name property at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM