Question

I have a global array variable that is enumerated with 'a'..'z'. If I wanted to initialize some entries in the variable declaration, I'd usually do the following:

var 
  Example: Array['a'..'z'] of String = ('Example one', 'two', 'three');

Is there a way to initialize just, say, 'g', 'p', 'o', and 'y', right there in the variable declaration, or do I have to do that via some function/procedure in the implementation part?

Might there even be a much smarter way to achieve what I want?

Was it helpful?

Solution

You cannot initialize only certain elements right there. The correct way of doing this would be in the initialization section, written after the implementation:


implementation
...
initialization
  Example['g'] := 'something';

end.

OTHER TIPS

There is simply no way to specify that you want just specific elements initialized. The initializer for a global array variable is simply a list of elements. The elements are specified in order. Any unspecified elements are set to default values, '' in your case. Because the list is processed in order, it follows that unspecified elements must be those at the end of the array.

In order to do that you must write those initializations in code.

var 
  Example: Array['a'..'z'] of String; // global variable
....
// and later in code
Example['a'] := ...;
Example['b'] := ...;
... etc.

Naturally you can pick any element you like in the code.

If you wish the initialization to be carried out as program startup time then you must invoke the initialization code from an initialization section.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top