Pass a multidimensional array as a parameter in Delphi

I would like to pass a multidimensional array to the constructor, for example:

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

Where LocalField is an array of an Integer array.

However, the above code will not compile ("ID is expected, but found ARRAY"). Can someone explain to me why this is wrong? I tried reading on open, static, and dynamic arrays, but have not yet found something that works. Is there a way to fix this without changing the type of LocalField?

+3
source share
4 answers

Create a specific type for the local field, then set it to MyParameter type, something like strings:

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

var
  LocalField: TTheArray;

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

(: , )

, pascal-

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

, , . .

+14

Delphi , , :

type
  TIntArray = array of Integer;

...

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

If the type is used earlier, as indicated in the answer, please note that you are passing it as a link, see

https://blog.spreendigital.de/2016/08/01/pass-a-multidimensional-array-as-a-parameter-with-a-hidden-caveat/

+1
source

I prefer it

procedure MakeMat(var c: TMatrix; nr, nc: integer; a: array of double);
var
  i, j: integer;
begin
  SetLength(c, nr, nc);
  for i := 0 to nr-1 do
    for j := 0 to nc-1 do
      c[i,j] := a[i*nc + j];
end;


MakeMat(ya, 5, 11,
       [1.53,1.38,1.29,1.18,1.06,1.00,1.00,1.06,1.12,1.16,1.18,
        0.57,0.52,0.48,0.44,0.40,0.39,0.39,0.40,0.42,0.43,0.44,
        0.27,0.25,0.23,0.21,0.19,0.18,0.18,0.19,0.20,0.21,0.21,
        0.22,0.20,0.19,0.17,0.15,0.14,0.14,0.15,0.16,0.17,0.17,
        0.20,0.18,0.16,0.15,0.14,0.13,0.13,0.14,0.14,0.15,0.15]);
0
source

Source: https://habr.com/ru/post/1723621/


All Articles