views:

182

answers:

2

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?

+8  A: 

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;
Gamecat
Should be a var parameter?
mghie
Good point. Corrected it.
Gamecat
+5  A: 

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".

Ulrich Gerhardt
Thanks Gerhardt. I'm still learning this stuff. :)
conciliator