Delphi: SetLength () in an argument of type "array TObject"

I am trying to resize an array of a specific class passed as an argument, e.g.

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

However, this causes the error "E2008 Incompatible Types". Is it true that you cannot do this (I saw rumors, but no official documentation), or am I doing something wrong?

+6
object arrays resize delphi
source share
2 answers

You did not specify the type explicitly. Thus, the compiler has problems corresponding to them. If you define a type type:

 type TObjectArray = array of TObject; 

There is no doubt about that and (thanks to Mghie), you should use the var parameter, because a change can cause a pointer change.

 procedure Resize(var MyArray: TObjectArray); begin SetLength(MyArray, 100); end; 
+10
source share

You mix open arrays (the Resize parameter) and dynamic arrays (which is expected by SetLength). See here for an explanation - especially the part called "Confusion".

+9
source share

All Articles