Search code examples
delphidelphi-7record

Why i can't use Variable in TRecord?


I'm with a doubt about the TRecord use.

Can i use a VAR in a record property?

For example:

My record:

TStyleEvalue = record
    ID: Integer;
    Value: TFontStyles;
    Name: String;

  end;

When i try to attribute VAR in the Name property as the code below:

StylesEvalueArray : array[0..15] of TStyleEvalue = (
    (ID: 00; Value: [];                                        Name: LB_NORMAL),
    (ID: 01; Value: [fsBold];                                  Name: LB_NEGRITO),
    (ID: 02; Value: [fsItalic];                                Name: LB_ITALICO),
    (ID: 03; Value: [fsBold,fsItalic];                         Name: LB_NEGRITO+', '+LB_ITALICO),
    (ID: 04; Value: [fsUnderline];                             Name: LB_SUBLINHADO),
    (ID: 05; Value: [fsBold,fsUnderline];                      Name: LB_NEGRITO+', '+LB_SUBLINHADO),
    (ID: 06; Value: [fsItalic,fsUnderline];                    Name: LB_ITALICO+', '+LB_SUBLINHADO),
    (ID: 07; Value: [fsBold,fsItalic,fsUnderline];             Name: LB_NEGRITO+', '+LB_ITALICO+', '+LB_SUBLINHADO),
    (ID: 08; Value: [fsStrikeOut];                             Name: LB_TACHADO),
    (ID: 09; Value: [fsBold,fsStrikeOut];                      Name: LB_NEGRITO+', '+LB_TACHADO),
    (ID: 10; Value: [fsItalic,fsStrikeOut];                    Name: LB_ITALICO+', '+LB_TACHADO),
    (ID: 11; Value: [fsBold,fsItalic,fsStrikeOut];             Name: LB_NEGRITO+', '+LB_ITALICO+', '+LB_TACHADO),
    (ID: 12; Value: [fsUnderline,fsStrikeOut];                 Name: LB_SUBLINHADO+', '+LB_TACHADO),
    (ID: 13; Value: [fsBold,fsUnderline,fsStrikeOut];          Name: LB_NEGRITO+', '+LB_SUBLINHADO+', '+LB_TACHADO),
    (ID: 14; Value: [fsItalic,fsUnderline,fsStrikeOut];        Name: LB_ITALICO+', '+LB_SUBLINHADO+', '+LB_TACHADO),
    (ID: 15; Value: [fsBold,fsItalic,fsUnderline,fsStrikeOut]; Name: LB_NEGRITO+', '+LB_ITALICO+', '+LB_SUBLINHADO+', '+LB_TACHADO)
  );

I get this error message:

[Error] FormFontChange.pas(102): Constant expression expected

This error happens at all the lines of this array.

Can anyone help me?

Thanks in advance.


Solution

  • You are declaring a typed constant. The values that you use for the typed constant must be what is known as constant expressions. The documentation can be found here: http://docwiki.embarcadero.com/RADStudio/en/Declared_Constants#Typed_Constants

    For the example here, the relevant section is that covering record constants. The documentation says:

    To declare a record constant, specify the value of each field - as fieldName: value, with the field assignments separated by semicolons - in parentheses at the end of the declaration. The values must be represented by constant expressions.

    The compiler is telling you that LB_NORMAL is not a constant expression. We don't know what LB_NORMAL is, but it must be a constant expression compatible with the type string. For instance:

    const
      LB_NORMAL = 'foo';
    

    would suffice. Or even:

    const
      foo = 'foo';
      bar = 'bar';
      LB_NORMAL = foo + bar;
    

    However you define LB_NORMAL, it is not a constant expression. You may need to consult the documentation for constant expressions to understand how to proceed.

    You seem to imply in the text, although it is not clear and sadly you did not show what LB_NORMAL is, that LB_NORMAL is a variable. Well, a variable is not a constant expression. If LB_NORMAL is indeed a variable, you will have to declare StylesEvalueArray as a variable also.