2

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] of 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'?

3
  • You have to do as in Donnie's answer to your old question. Commented Feb 3, 2011 at 15:44
  • 3
    It's excusable to ask an exact duplicate of somebody else's question, but it's pretty poor form to ask your own question again! Commented Feb 3, 2011 at 15:54
  • 1
    I think it's poor not to see that the old question was not my question and to say that there is no other answer just because you don't know one. Serg gives me a perfect solution for MY problem. Commented Feb 4, 2011 at 7:01

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

+1 I didn't know you could call Create on a dynamic array!!
@David: I am sure the feature exists in Delphi 2007, maybe in earlier versions.
@David: It was added in Delphi 6 or 7 (I know it works in 7). It's a slick way to initialize constant arrays too without knowing ahead of time how many elements there will be. MyIntArray := TIntArray.Create(1, 2, 3, 12, 23, 34, 96, 57, 106); sure beats 'const MyIntArray: array[0..8] of Integer = (1, 2,...);` - adding a new entry to the second example requires updating both the array bounds and the array itself, while the first only requires adding an element to the array.
@Ken You don't need to explain the benefits to me – they are manifest!!! Not present in D6, I just checked. Too bad you can't pass an open array to one of these constructors, that's how I mostly would want to use this.
@David: I think I was wrong about the timing, though... I think the construct was added in D2006 or D2007, not D6 or 7.
|
2

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.