开发者

Pass multidimensional array as argument in Delphi [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Pass a multidimensional array as a parameter in Delphi

Hi all, same question as here: Pass a multidimensional array as a parameter in Delphi

but need for another answer.

type
MultiArray = array of array of Integer;

procedure Foo(a : MultiArray);
begin

end;

procedure Bar(a : array of Integer);
var i : Integer;
begin
  for i in a do WriteLn(IntToStr(i));
end;

const
    a : array[0..2] of Integer = (1, 2, 3);
    ma : array[0..1] 开发者_如何学Cof array[0..1] of Integer = ((1,2),(3,4));
begin
    Bar(a);
    Bar([1,2,3]);
    //Foo(ma);
end.

I want to pass arrays of different sizes to Foo. If its not possible this way, any 'workarounds'?


I guess that the question is about multidimensional open arrays.

There are no multidimensional open arrays in Delphi. But you can create a single-dimensional open array with dynamic arrays as elements. Here is an example:

type
  TIntArray = array of Integer;

procedure Test(arr: array of TIntArray);
begin
  ShowMessage(IntToStr(arr[1,1]));
end;

procedure TForm9.Button5Click(Sender: TObject);
begin
  Test([TIntArray.Create(1,2), TIntArray.Create(2,3)]);
end;


I don't get what you want to do. You write

I want to pass arrays of different sizes to Foo.

Well, why don't you do that, then?

If

type
  TMultiArray = array of array of integer;

and

procedure Foo(a: TMultiArray);

then you can do

var
  ShortMultiArray, LongMultiArray, HugeMultiArray: TMultiArray;

begin
  SetLength(ShortMultiArray, 10, 10);
  SetLength(LongMultiArray, 100, 100);
  SetLength(HugeMultiArray, 1000, 1000);

  Foo(ShortMultiArray);
  Foo(LongMultiArray);
  Foo(HugeMultiArray);
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜