views:

535

answers:

2

I'd like to pass a multi-dimensional array to a constructor like so:

constructor TMyClass.Create(MyParameter: array of array of Integer);
begin
  LocalField := MyParameter;
end;

Where LocalField is an array of array of Integer.

However the above code won't compile ('Identifier expected but ARRAY found'). Could somebody explain to me why this is wrong? I tried reading up on open, static and dynamic arrays but have yet to find something that works. Is there a way to fix it without changing the type of LocalField?

+8  A: 

Make a specific type for localfield, then set that as the type of MyParameter, something along the lines of:

type
  TTheArray = array[1..5] of array[1..10] of Integer;

var
  LocalField: TTheArray;

constructor TMyClass.Create(MyParameter: TTheArray);
...

(Note: not verified in a compiler, minor errors may be present)

Note that most often in pascal-like syntax a multidimensional array is more properly declared as

type
  TTheArray = array[1..5, 1..10] of Integer;

Unless, of course, you have some good reason for doing it the other way.

Donnie
+1. When in doubt, *name your types*.
Rob Kennedy
+1  A: 

I don't have Delphi at hands, but I think this should work:

type
  TIntArray = array of Integer;

...

constructor TMyClass.Create (MyParameter : array of TIntArray);
begin
...
end;
Smasher