Pass an array of array as a parameter

165 Views Asked by At

I have such an array:

12 |  18  |  22   |   26   |   28  |  32   | 34  | 36   |  50 
16           23                       33           37 

As you can see, it consists of two rows, but some have one element and some have two.

I'm trying to write a function that takes an array of arrays as a parameter.

I declared myself a type:

type array_of_array_of_Byte = array of array of Byte;

function my_function(value_list: array_of_array_of_Byte)

And I call the function like this:

my_function([[12,16], [18], [22,23], [26], [28], [32,33], [34], [36,37], [50]]);

But I get error:

Ordinal type required

What am I doing wrong? I'm using Delphi XE4.

1

There are 1 best solutions below

0
Remy Lebeau On

Unfortunately, the ability to initialize a dynamic array from a constant array expression wasn't supported yet in XE4, it was added in XE7:

Dynamic Arrays in Delphi XE7

There is a significant new feature in the Object Pascal language for XE7 and that is the improved support for initializing dynamic arrays and operating on them.

...

So what's new in XE7? The Object Pascal language allows you to do a few new operations:

  • Assign a constant array to a dynamic array
  • Concatenate two constant arrays with the + symbol
  • Use the following RTL functions on any dynamic array (not just strings): Insert, Delete, and Concat

So, in your case, you have no choice but to simply build up your dynamic arrays manually the old fashioned way - using SetLength() and per-element assignments, eg:

type
  array_of_array_of_Byte = array of array of Byte;

procedure my_function(value_list: array_of_array_of_Byte);

... 

var
  arr: array_of_array_of_Byte;
begin
  SetLength(arr, 9);

  SetLength(arr[0], 2);
  arr[0][0] := 12;
  arr[0][1] := 16;

  SetLength(arr[1], 1);
  arr[1][0] := 18;

  // and so on ...

  my_function(arr);
end