0
votes
procedure TForm1.controlClick(Sender: TObject);
var
  i: Integer;
begin
  for i := 2 to Dest.Count-1 do
  begin
    img[i-2].Create(Form1);
    with img[i-2] do begin
      Parent:= Panel1;
      Width:= 100;
      Height:= 150;
      Top:= 10;
      Left:= (i-2)*100;
    end;
  end;
end;

img type is array of TImage, control is a tab. I want to timages to show like an android gallery. But this gives me an error Access Violation.

1
where do you define the size of the array? - James Barrass
Your img[i-2] is uninitialized. - Free Consulting
@JamesBarrass i defined like img: array of TImage; ( i tried to create dynamic size) - user3142206
you need to call SetLength on it prior to using it to define its size. - James Barrass
img.SetLength(Dest.Count); ? @JamesBarrass - user3142206

1 Answers

2
votes

This looks like the classic error in creating an object. Instead of

obj.Create;

you must write:

obj := TSomeClass.Create;

In your case you need to first of all allocate the array:

SetLength(img, Dest.Count-2);

And then in the loop you write:

img[i-2] := TImage.Create(Form1);

to instantiate the images.