Initialize a constant, which is a dynamic array of fixed arrays?

Is it possible to initialize a constant dynamic array of arrays?

If a:

type tNamePair = array[1..2] of String; tPairList = array of tNamePair; 

How to create an initialized constant? I can not get the code below:

 const PairList: tPairList = ( ('One', '1'), ('Two', '2'), ('Three', '3')); 

If this is not possible, you can initialize a constant fixed array using a fixed array:

  type tPairList: array[1..3] of tNamePair; 

If this is not possible, you can initialize a permanent dynamic array with the entry:

 tNamePair = record English: String; Number: String; end; tPairList = array of tNamePair; 

If this is not possible, you can initialize the constant fixed array by writing:

 tNamePair = record English: String; Number: String; end; tPairList = array[1..3] of tNamePair; 

If this is not possible, any suggestions other than just assignments in the code that, frankly, would take less time than compiling this question!

+8
delphi
source share
3 answers

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.

+22
source share

In XE7, you can declare dynamic array constants.

Simple case:

 const a: TArray<String> = ['Delphi','XE7']; 

In your example, this compiles:

 type tNamePair = TArray<String>; tPairList = TArray<tNamePair>; const PairList: tPairList = [['One', '1'],['Two', '2'],['Three', '3']]; 

To create a dynamic record array, it can be executed at runtime as follows:

 Type TNamePair = record English: String; Number: String; class function Define(_English,_Number: String): TNamePair; static; end; TPairList = TArray<TNamePair>; class function TNamePair.Define(_English, _Number: String): TNamePair; begin Result.English := _English; Result.Number := _Number; end; var pl : TPairList; begin pl := [TNamePair.Define('A','1'),TNamePair.Define('B','2')]; ... end; 
+8
source share

You can use this method:

 const C_ARRAY_CODE : array ['a'..'d'] of string = ('01','02','03','04'); var Conter:Char; begin //Use With Loop for Conter := Low(C_ARRAY_CODE) to high(C_ARRAY_CODE) do ShowMessage(C_ARRAY_CODE[Conter]); //Use Direct ShowMessage(C_ARRAY_CODE['a']); end; 
0
source share

All Articles