Delphi – Declaration Matrix Const

Edit my question.

I will be specific.

How to declare the following code as const instead of var?
(I can’t get the 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';

in the code The specific problem is that the array you declare is dynamic. That is, the boundary is not fixed and can be changed at runtime.

In the old version of Delphi (XE6 and earlier), it is fundamentally It is not possible to declare dynamic array constants. In XE7 and later, it is possible, but the syntax is different from fixed array constants.

In all versions, if the declaration has a specified (and therefore fixed) boundary Constant array, you can specify the content of the constant array:

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','F 7'),
('G0','G1','G2','G3','G4','G5','G6','G7'),
('H0' ,'H1','H2','H3','H4','H5','H6','H7')
);

If your array needs to be earlier than XE6 is dynamic in the Delphi version, so you cannot use such a declaration to initialize such an array.

If you are using Delphi XE7 or later, you can use alternate syntax to declare dynamic array constants This is very similar to the syntax of fixed array constants, but use square brackets [] instead of regular brackets ():

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 if you use Dynamic array, if you have some initial state (boundary and content) and you want to initialize it, then you can use a fixed array constant to define the initial state, and then initialize the dynamic array from the constant at runtime, such as:

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;

Edit me Question.

I will be specific.

How to declare the following code as const instead of var?
(I can’t get the 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';

The specific problem in the code is that the array you declare is dynamic. In other words , The boundary is not fixed and can be changed at runtime.

In older versions of Delphi (XE6 and earlier), it was impossible to declare dynamic array constants. In XE7 and later , It is possible, but the syntax is different from fixed array constants.

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

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 Delphi version earlier than XE6, then you cannot use such a declaration to initialize such an array.

If you are using Delphi XE7 or In later versions, you can use alternate syntax to declare dynamic array constants. This is very similar to the syntax of fixed array constants, but use square brackets [] instead of regular brackets ():

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']
];

Mixed solution for the old Delphi version

If You are using an older version of Delphi, so even if you use a dynamic array, if you have some initial state (boundary and content) and you want to initialize it, then you can use a fixed array constant to define that initial state, and then from that Constants initialize dynamic arrays at runtime, such as:

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;

Leave a Comment

Your email address will not be published.