Pass a multidimensional array as a parameter in Delphi

前端 未结 4 1396
你的背包
你的背包 2021-01-20 15:17

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         


        
相关标签:
4条回答
  • 2021-01-20 15:24

    If a type is used before as suggested in the answer, please note that you are passing it as a reference, see:

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

    0 讨论(0)
  • 2021-01-20 15:29

    I prefer this

    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 讨论(0)
  • 2021-01-20 15:35

    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;
    
    0 讨论(0)
  • 2021-01-20 15:38

    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.

    0 讨论(0)
提交回复
热议问题