gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:42:20 27 4
gpt4 key购买 nike

我对 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 声明为变量。

关于delphi - 为什么我不能在 TRecord 中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com