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 开发者_JAVA技巧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?
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;
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".
精彩评论