dynamic multidimensional array

Today I got a task to convert as C# desktop application into a Delphi XE application. One thing I learned is about dynamic multidimensional array. In C#, we can do this by just simply using one-liner “new” command and state the array dimension with it. As in Delphi, this is not the case, since all variables had to be declared far-before used.

Dynamic multidimensional array in Delphi can be declared using iterated “array of” keyword

Inside “begin“..”end” of a procedure/function that using the array, we can determine or change the array dimension like this:

That command will set myInt into 3×3 dimension array. Apparently, Delphi also allowed you to have a multidimensional array that has different number of column for each row, for example:

The first row will made myInt array have 2 rows without specifying how may columns. Second line will set the first row to have 3 columns and third line will set the second row of myInt to have 2 columns.

To check how many row or column a multidimensional array had, you can use “Length” command

Destroying a dynamic multidimensional array can be done by assigning the array to nil

You can’t use dynamic multidimensional array as procedure/function parameter or result type directly. You’ll get error if you do this:

Instead of that, you had to define your own variable type and until then you can use it as function parameter or result type.

Using your own variable type in function:

Important Notes: if you work on 2 dimensional array that related to object that had table (such as stringgrid) or image/bitmap coordinate, you had to switch the term “row” and “column” I described above. Delphi use this: table_name[column, row];.

Leave a Reply

%d bloggers like this: