2

I declared two things in my var section for this Delphi program:

  LCloneTask1, LCloneTask2, LCloneTask3, ... , LCloneTask20 : String;
  CloneTaskArray : array[0..19] of String = (LCloneTask1, LCloneTask2, LCloneTask3,.., LCloneTask20);

Where the first line declares some strings and the second line declares an array of said strings. This array is always going to be 20 in size.

When I try to compile I get an error, "Constant expression expected". Am i not declaring a string array properly? I need to be able to read from and write to this array later in the program.

5
  • You don't need the first line at all. Just CloneTaskArray : array[0..19] of String; Commented Apr 29, 2013 at 18:34
  • If i don't include the first list, I get a bunch of red Undeclared Identifier warnings on the array variable names... Commented Apr 29, 2013 at 18:40
  • Use either the array or the variables, not both. You're defining two different places in the memory for two different strings of the same purpose. Rather than reading LCloneTask1 instead read CloneTaskArray[0]. Or, as David describes below, use pointers to those strings. Commented Apr 29, 2013 at 18:41
  • Ah, i think i got it. my brain wasn't quite catching up to what you said. CloneTaskArray : array[0..19] of String; Commented Apr 29, 2013 at 18:44
  • See this one. Commented Apr 29, 2013 at 18:48

1 Answer 1

3

You are trying to initialize a variable as part of its declaration. The documentation states that the syntax must be:

var identifier: type = constantExpression;

where constantExpression is any constant expression representing a value of type type.

The documentation for constant expressions says (emphasis mine):

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. Constant expressions cannot include variables, pointers, or function calls.

You are contravening the final sentence, specifically the part that I have highlighted.


It's quite possible that all you want to do is declare an array of strings. In which case you would simply write:

var
  CloneTaskArray: array[0..19] of string;

If you need to initialize this array, do so in the initialization section of the unit that declares them:

initialization
  CloneTaskArray[0] := 'boo';
  CloneTaskArray[1] := 'yah';
  ....

I note that you are trying to initialize the elements of the array with other string variables. With a simpler example, I wonder if you are trying to do this:

var
  s1, s2: string;
  StringArray: array[0..1] of string;
....
StringArray[0] := s1; 
StringArray[1] := s2; 

and then I wonder if you are hoping that you can do this:

s1 := 'boo';
Assert(StringArray[0] = 'boo');

If that's what you are hoping for, you will be disappointed. The string data type in Delphi is quite complex, but fundamentally it behaves like a value type. If you are trying to do what I outline above you'll need to use references to string variables:

type
  PString = ^string;
var
  s1, s2: string;
  StringArray: array[0..1] of PString;
....
StringArray[0] := @s1; 
StringArray[1] := @s2; 

and then you can indeed write:

s1 := 'boo';
Assert(StringArray[0]^ = 'boo');
Sign up to request clarification or add additional context in comments.

4 Comments

while i think i understand the reasoning on why i/the program cannot initialize a constant expression in the way that i've done it, i still need to try and create the array in the way i described (r/w during the program). is this saying that it cannot be done?
I don't know what you mean.
your comment of using CloneTaskArray : array[0..19] of string; as the declaration was sufficient for me to grasp, thanks.
@ikathegreat it appears that what you want to do is use the pointers, that is, if you really do need to have both the variables and the array. Otherwise, choose one or the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.