为什么我不能在 TRecord 中使用变量?

Why i can't use Variable in TRecord?

我对 TRecord 的使用有疑问。

我可以在记录中使用 VAR 属性吗?

例如:

我的记录:

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

  end;

当我尝试将 Name 属性 中的 VAR 归因于以下代码时:

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)
  );

我收到此错误消息:

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

这个错误发生在这个数组的所有行。

谁能帮帮我?

提前致谢。

您正在声明类型常量。用于类型常量的值必须是所谓的常量表达式。可以在此处找到文档:http://docwiki.embarcadero.com/RADStudio/en/Declared_Constants#Typed_Constants

对于此处的示例,相关部分是涵盖 record constants 的部分。文档说:

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.

编译器告诉您 LB_NORMAL 不是常量表达式。我们不知道LB_NORMAL是什么,但一定是一个兼容string类型的常量表达式。例如:

const
  LB_NORMAL = 'foo';

就足够了。甚至:

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

无论您如何定义 LB_NORMAL,它都不是常量表达式。您可能需要查阅 constant expressions 的文档以了解如何继续。

你似乎在文中暗示,虽然不清楚,遗憾的是你没有说明 LB_NORMAL 是什么,LB_NORMAL 是一个变量。好吧,变量不是常量表达式。如果 LB_NORMAL 确实是一个变量,你也必须将 StylesEvalueArray 声明为一个变量。