Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Declare Matrix const

Tags:

delphi

Editing my Question.

I'll be specific.

How can I declare the code below as const instead of var? (I couldn't get Cube example)

 var
 Matrix : array of array of string;

 SetLength(Matrix, 8, 8);
 Matrix[0,0]:='A0';Matrix[0,1]:='A1';Matrix[0,2]:='A2';Matrix[0,3]:='A3';Matrix[0,4]:='A4';Matrix[0,5]:='A5';Matrix[0,6]:='A6';Matrix[0,7]:='A7';
 Matrix[1,0]:='B0';Matrix[1,1]:='B1';Matrix[1,2]:='B2';Matrix[1,3]:='B3';Matrix[1,4]:='B4';Matrix[1,5]:='B5';Matrix[1,6]:='B6';Matrix[1,7]:='B7';
 Matrix[2,0]:='C0';Matrix[2,1]:='C1';Matrix[2,2]:='C2';Matrix[2,3]:='C3';Matrix[2,4]:='C4';Matrix[2,5]:='C5';Matrix[2,6]:='C6';Matrix[2,7]:='C7';
 Matrix[3,0]:='D0';Matrix[3,1]:='D1';Matrix[3,2]:='D2';Matrix[3,3]:='D3';Matrix[3,4]:='D4';Matrix[3,5]:='D5';Matrix[3,6]:='D6';Matrix[3,7]:='D7';
 Matrix[4,0]:='E0';Matrix[4,1]:='E1';Matrix[4,2]:='E2';Matrix[4,3]:='E3';Matrix[4,4]:='E4';Matrix[4,5]:='E5';Matrix[4,6]:='E6';Matrix[4,7]:='E7';
 Matrix[5,0]:='F0';Matrix[5,1]:='F1';Matrix[5,2]:='F2';Matrix[5,3]:='F3';Matrix[5,4]:='F4';Matrix[5,5]:='F5';Matrix[5,6]:='F6';Matrix[5,7]:='F7';
 Matrix[6,0]:='G0';Matrix[6,1]:='G1';Matrix[6,2]:='G2';Matrix[6,3]:='G3';Matrix[6,4]:='G4';Matrix[6,5]:='G5';Matrix[6,6]:='G6';Matrix[6,7]:='G7';
 Matrix[7,0]:='H0';Matrix[7,1]:='H1';Matrix[7,2]:='H2';Matrix[7,3]:='H3';Matrix[7,4]:='H4';Matrix[7,5]:='H5';Matrix[7,6]:='H6';Matrix[7,7]:='H7';
like image 423
Flavio Correia Avatar asked Dec 15 '22 04:12

Flavio Correia


2 Answers

The specific problem in your code is that the array you are declaring is dynamic. That is, the bounds are not fixed and can be changed at run-time.

In older versions of Delphi (XE6 and earlier) is it simply not possible to declare dynamic array constants. In XE7 and later it is possible but the syntax is different than for fixed array constants.

In all versions, if you declare a constant array with specified (and therefore fixed) bounds you can then specify the contents of the constant array thus:

const
  Matrix : array[0..7, 0..7] of String = 
   (
    ('A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'),
    ('B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'),
    ('C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'),
    ('D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'),
    ('E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7'),
    ('F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'),
    ('G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'),
    ('H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7')
   );

If your array needs to be dynamic in a version of Delphi earlier than XE6 then you cannot initialise such an array with a declaration like this.

If you are using Delphi XE7 or later, then you can use the alternate syntax for declaring a dynamic array constant. This is very similar to the syntax for a fixed array constant but uses square braces [] instead of regular parentheses ():

 const
   Matrix : array of array of String =
   [
    ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'],
    ['B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'],
    ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'],
    ['D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'],
    ['E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7'],
    ['F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'],
    ['G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'],
    ['H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7']
   ];

Hybrid Solution for Older Delphi Versions

If you are using an older version of Delphi then even with a dynamic array, if you have some initial state (bounds and content) that you would like to initialise it with then you could use a fixed array constant to define that initial state and then initialise your dynamic array at run-time from that constant, something like:

const
  MX_DIM  = 8;
  MX_DEFAULT : array[0..MX_DIM - 1, 0..MX_DIM - 1] of String =
   (
    ('A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'),
    ('B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'),
    ('C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'),
    ('D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'),
    ('E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7'),
    ('F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'),
    ('G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'),
    ('H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7')
   );

// Then in your code:

var
  x, y: Integer;
  Matrix: array of array of String;
begin
  // Initialise 'Matrix' from MX_DEFAULT:

  SetLength(Matrix, MX_DIM, MX_DIM);
  for x := 0 to Pred(MX_DIM) do
    for y := 0 to Pred(MX_DIM) do
      Matrix[x, y] := MX_DEFAULT[x, y];
end;
like image 94
Deltics Avatar answered Dec 22 '22 00:12

Deltics


The documentation shows how to declare constant arrays

Array Constants

To declare an array constant, enclose the values of the elements of the array, separated by commas, in parentheses at the end of the declaration. These values must be represented by constant expressions. For example:

const Digits: array[0..9] of Char =
  ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

declares a typed constant called Digits that holds an array of characters.

To define a multidimensional array constant, enclose the values of each dimension in a separate set of parentheses, separated by commas. For example:

type 
  TCube = array[0..1, 0..1, 0..1] of Integer;

const 
  Maze: TCube = (((0, 1), (2, 3)), ((4, 5), (6,7)));

creates an array called Maze where:

Maze[0,0,0] = 0
Maze[0,0,1] = 1
Maze[0,1,0] = 2
Maze[0,1,1] = 3
Maze[1,0,0] = 4
Maze[1,0,1] = 5
Maze[1,1,0] = 6
Maze[1,1,1] = 7

These examples are for one dimensional and three dimensional arrays. For a two dimensional array it would be:

const
  Coords: array [0..2, 0..2] of string = (
    ('A0', 'A1', 'A2'),
    ('B0', 'B1', 'B2'),
    ('C0', 'C1', 'C2'),
  );
like image 37
David Heffernan Avatar answered Dec 22 '22 00:12

David Heffernan