48

Is there a way in Delphi declaring an array of strings such as following one?

{'first','second','third'}
bluish
  • 26,356
  • 27
  • 122
  • 180
none
  • 4,669
  • 14
  • 62
  • 102

4 Answers4

62

In XE7 you can declare a dynamic array constant like this:

const
  MyArray: TArray<String> = ['First','Second','Third'];
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • 1
    Please note that due to the compiler bug documented in [RSP-21151](https://quality.embarcadero.com/browse/RSP-21151) this currently doesn't work for class constants (fails with `E2086 Type 'TArray' is not yet completely defined`), unless you add an additional: `type TStringArray = TArray;` to your unit. | (Note that it _must_ be in a separate `type` section to prevent the bug and that the workaround doesn't seem to work for generic classes. See the ticket for all details.) – Marcus Mangelsdorf Nov 04 '20 at 13:55
58

try this

Const
Elements =3;
MyArray  : array  [1..Elements] of string = ('element 1','element 2','element 3');
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 2
    thanks, thow i was hopping to avoid the use of Elements and let the Delphi calculate the size of the array. – none Nov 01 '10 at 15:54
  • 5
    You can avoid the Elements constant, but you have to declare the array size anyway. You can use the "short form" array[x] where x is an enumerated type, creating an array from the first to the last element of the type. The compiler does not support array[] = (1,2,3) calculating the array size and setting a 0..2 boundary automatically. –  Nov 02 '10 at 11:10
  • 1
    Brilliant, all this time I thought this wasn't possible :D – Jerry Dodge Feb 05 '15 at 03:18
22

You can use dynamic arrays and try this:

var
  FMyArray: TArray<string>;

function MyArray: TArray<string>;
begin
  if Length(FMyArray) = 0 then
    FMyArray := TArray<string>.Create('One', 'Two', 'Three');
  Result := FMyArray;
end;

While this does do a run-time initialization of a dynamic array on the heap, it also shows that Delphi supports a "pseudo-constructor" on dynamic arrays that allow in-place initialization. (NOTE: the above code isn't thread-safe).

Now all you need to do to find out the length of the array, is use the Length() standard function, or to find the allowed index range, use the Low() and High() standard functions.

If you're using an older version of Delphi, replace the TArray with your own dynamic-array string type such as:

type
  TStringArray = array of string;
Allen Bauer
  • 16,657
  • 2
  • 56
  • 74
  • 1
    true, that is valid, however it needs to be const and not runtime. – none Nov 02 '10 at 11:06
  • 1
    The elements are constant, only the array is not. Other than a startup cost, there is very little runtime overhead. Does it need to be const merely because of tradition or is there something specific to your case that requires const? – Allen Bauer Nov 02 '10 at 16:05
  • 3
    if the array is not const then the array could change. does not fit requirements. the idea is nice tho. – none Nov 03 '10 at 11:52
2

You can do this in a indirect way. Create a function like:

procedure assignStringArray(var rasVelden: ArrayOfString; const asVeldenIn: Array Of String);
var
   iLengte, iT1: Integer;
begin
   iLengte := Length(asVeldenIn);
   SetLength(rasVelden, iLengte);
   for iT1 := iLengte-1 downto 0 do
      rasVelden[iT1] := asVeldenIn[iT1];
end;

and call this function like:

assignStringArray(asVelden, ['String1', 'String2', 'String3']);

where:

asVelden: ArrayOfString;