Prior to XE7, you could not create dynamic array constants. Constants must be known at compile time, but dynamic arrays are allocated at runtime.
A fixed array of fixed arrays can be declared at compile time:
type tNamePair = array[1..2] of String; tPairList = array[1..3] of tNamePair; const PairList: tPairList = ( ('One', '1'), ('Two', '2'), ('Three', '3'));
A fixed array of records can also be declared at compile time:
type tNamePair = record English: String; Number: String; end; tPairList = array[1..3] of tNamePair; const PairList: tPairList = ( (English: 'One'; Number: '1'), (English: 'Two'; Number: '2'), (English: 'Three'; Number: '3'));
If you need a dynamic array, you must build it at runtime. You can create it directly:
type tNamePair = array[1..2] of String; tPairList = array of tNamePair; var PairList: tPairList; initialization SetLength(PairList, 3); PairList[0][1] := 'One'; PairList[0][2] := '1'; PairList[1][1] := 'Two'; PairList[1][2] := '2'; PairList[2][1] := 'Three'; PairList[2][2] := '3'; end.
Or you can define a fixed array of compile-time constant and copy it to a dynamic array at runtime:
type tNamePair = array[1..2] of String; tPairList = array[1..3] of tNamePair; tPairListDyn = array of tNamePair; const PairList: tPairList = ( ('One', '1'), ('Two', '2'), ('Three', '3')); function MakePairListDyn(const Pairs: tPairList): tPairListDyn; var I, J: Integer; begin SetLength(Result, Length(Pairs)); for I := Low(Pairs) to High(Pairs) do begin Result[J] := Pairs[I]; Inc(J); end; end; var Pairs: tPairListDyn; initialization Pairs := MakePairListDyn(PairList); end.
For the XE7 post situation, see @LURD answer below.
Remy Lebeau
source share