Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: SetLength() on argument of type "array of TObject"

I'm trying to resize an array of a certain class passed as an argument, e.g.

procedure Resize(MyArray: Array of TObject);
begin
  SetLength(MyArray, 100);
end;

However, this raises an error "E2008 Incompatible types". Is it true that you can't do this (I've seen rumors, but no official documentation) or am I doing something wrong?

like image 443
conciliator Avatar asked Oct 29 '09 08:10

conciliator


2 Answers

You didn't defined the type explicitly. So the compiler has problems matching them. If you define the type like:

type
  TObjectArray = array of TObject;

There is no doubt about it and (thanks to Mghie) you should be using a var parameter because resising likely cause a change in the pointer.

procedure Resize(var MyArray: TObjectArray);
begin
  SetLength(MyArray, 100);
end;
like image 80
Toon Krijthe Avatar answered Sep 28 '22 04:09

Toon Krijthe


You are mixing open arrays (the parameter of Resize) and dynamic arrays (what SetLength expects). See here for an explanation - especially the part titled "Confusion".

like image 33
Uli Gerhardt Avatar answered Sep 28 '22 03:09

Uli Gerhardt