1

This answer shows that you can create a constant generic array of strings. So this code works:

const AllTestJSON: TArray<String> = [
  '''
    [
      {'Computer Webcam', TAlphaColors.Aliceblue},
      {'Computer Monitor', TAlphaColors.Blueviolet},
      {'Computer Mouse', TAlphaColors.Aqua},
      {'Computer Keyboard', TAlphaColors.Blue},
    ]
  ''',
  '''
    [
      {'External Harddrive', TAlphaColors.Brown},
      {'USB Hub', TAlphaColors.Chartreuse},
      {'Computer Mousepad', TAlphaColors.Crimson}
    ]
  ''',
  '''
    [
      {'Computer Mouse', TAlphaColors.Aqua},
      {'Computer Keyboard', TAlphaColors.Blue},
      {'External Harddrive', TAlphaColors.Brown},
    ]
  '''
];

But I don't want a normal array of strings. I want an array of strings with constants like this:

const Test1: String = '''
  [
    {'Computer Webcam', TAlphaColors.Aliceblue},
    {'Computer Monitor', TAlphaColors.Blueviolet},
    {'Computer Mouse', TAlphaColors.Aqua},
    {'Computer Keyboard', TAlphaColors.Blue},
  ]
''';
const Test2: String = '''
  [
    {'External Harddrive', TAlphaColors.Brown},
    {'USB Hub', TAlphaColors.Chartreuse},
    {'Computer Mousepad', TAlphaColors.Crimson}
  ]
''';
const Test3: String = '''
  [
    {'Computer Mouse', TAlphaColors.Aqua},
    {'Computer Keyboard', TAlphaColors.Blue},
    {'External Harddrive', TAlphaColors.Brown},
  ]
''';
const AllTestJSON: TArray<String> = [Test1, Test2, Test3];

That however doesn't work and results in the error:

Constant expression expected

Is there a way to do what I am trying to do? I want my strings to be in constants and then put those constants into my array constant.

1
  • When you write const you don't have a variable - the whole point of a variable is that its content is variable (whereas a constant's content is constant). Nothing in your code is (a) variable. Commented Jun 9 at 21:06

2 Answers 2

3

Remove the : string parts on the individual string constants, making them be true constants instead of typed constants. True constants are more widely accepted; for instance, they can be used in "constant expressions", while typed constants can not.

const Test1 = '''
  [
    {'Computer Webcam', TAlphaColors.Aliceblue},
    {'Computer Monitor', TAlphaColors.Blueviolet},
    {'Computer Mouse', TAlphaColors.Aqua},
    {'Computer Keyboard', TAlphaColors.Blue},
  ]
''';

const Test2 = '''
  [
    {'External Harddrive', TAlphaColors.Brown},
    {'USB Hub', TAlphaColors.Chartreuse},
    {'Computer Mousepad', TAlphaColors.Crimson}
  ]
''';

const Test3 = '''
  [
    {'Computer Mouse', TAlphaColors.Aqua},
    {'Computer Keyboard', TAlphaColors.Blue},
    {'External Harddrive', TAlphaColors.Brown},
  ]
''';

const AllTestJSON: TArray<string> = [Test1, Test2, Test3];
Sign up to request clarification or add additional context in comments.

5 Comments

I would have also killed all the redundant const keywords. The JS(ON) like content of the literals don't matter either for this problem...
@AmigoJack: Quite true, but I didn't want to divert too much from the OP's original code.
Not related to your question, but still asking here, since you used a variable with JSON in its name and included those elaborate strings (which are JSON-like but not valid JSON): can such strings be used with some library to expand JSON-like strings and get values from Delphi somehow inserted?
@MatthiasB Short answer is yes, but long answer is it's just my own little one that I wrote and use in some projects. The actual syntax for it is ${variable_name} to put the value into the JSON string, but I don't have it like that in my question as I was just testing random json text. Ideally I'd want this functionality built into Delphi, but it's not. I've basically tried to make mine do what JavaScript Template Strings do: w3schools.com/js/js_string_templates.asp
I see. One might achieve something similar with Delphi's Format function. E.g. Format('Welcome %s, %s! The total price is: %f', [FirstName, LastName, Price * (1 + VAT)]) But, this is off-topic.
1

You can probably initialize AllTestJSON in an initialization section (Recommended for your scenario)

This is the most common and flexible way to achieve what you're trying to do I think. You declare AllTestJSON as a var (or class var if it's within a class and you want it to be static) and then initialize it in the initialization section of your unit. This section runs once when the unit is loaded, guaranteeing the array is populated before any other code in the unit uses it.

I didn't compile this, but something like this:

type
  // Declare your individual string constants
  const
    Test1: String =
    '''
      [
        {"Computer Webcam", TAlphaColors.Aliceblue},
        {"Computer Monitor", TAlphaColors.Blueviolet},
        {"Computer Mouse", TAlphaColors.Aqua},
        {"Computer Keyboard", TAlphaColors.Blue}
      ]
    ''';

    Test2: String =
    '''
      [
        {"External Harddrive", TAlphaColors.Brown},
        {"USB Hub", TAlphaColors.Chartreuse},
        {"Computer Mousepad", TAlphaColors.Crimson}
      ]
    ''';

    Test3: String =
    '''
      [
        {"Computer Mouse", TAlphaColors.Aqua},
        {"Computer Keyboard", TAlphaColors.Blue},
        {"External Harddrive", TAlphaColors.Brown}
      ]
    ''';

  // Declare AllTestJSON as a variable
  var
    AllTestJSON: TArray<String>;

implementation

initialization
  // Initialize the array using the string constants
  AllTestJSON := [Test1, Test2, Test3];
end.

Comments

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.