是否可能:记录中的数组

发布于 12-07 08:28 字数 400 浏览 1 评论 0原文

我有下一个数组

NAMES1: array[0..1] of string = ('NO1','NAME1');
NAMES2: array[0..1] of string = ('NO2','NAME2');

和一个记录结构

TMyRec = record(
  Name: ????;
);

结果我需要声明一个常量记录数组,如下所示

const
  StringArraysList: array[0..1] of TMyRec = (
    (Name: NAMES1),
    (Name: NAMES2)
  );

问题是我应该为 TMyRec 中的名称选择什么类型?

I have next arrays

NAMES1: array[0..1] of string = ('NO1','NAME1');
NAMES2: array[0..1] of string = ('NO2','NAME2');

and a record structure

TMyRec = record(
  Name: ????;
);

As result I need to declare a constant array of records like following

const
  StringArraysList: array[0..1] of TMyRec = (
    (Name: NAMES1),
    (Name: NAMES2)
  );

The question is what type should I select for Name in TMyRec?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我2024-12-14 08:28:56

你需要这样做:

type
  TTwoNames = array[0..1] of string;

  TMyRec = record
    Names: TTwoNames;
  end;

const
  StringArraysList: array[0..1] of TMyRec = (
    (Names: ('NO1','NAME1')),
    (Names: ('NO2','NAME2'))
  );

你更愿意将最终声明写为

const
  NAMES1: TTwoNames = ('NO1','NAME1');
  NAMES2: TTwoNames = ('NO2','NAME2');

  StringArraysList: array[0..1] of TMyRec = (
    (Names: NAMES1),
    (Names: NAMES2)
  );

但这会导致

[DCC 错误] Unit1.pas(38): E2026 需要常量表达式

某些 Delphi 常量并不像您希望的那样常量!

记录常量的文档指出

这些值必须由常量表达式表示。

类型化常量的文档指出

类型常量不能出现在常量表达式中。

将这两个规则放在一起,我们就得到了 E2026

You need to do it like this:

type
  TTwoNames = array[0..1] of string;

  TMyRec = record
    Names: TTwoNames;
  end;

const
  StringArraysList: array[0..1] of TMyRec = (
    (Names: ('NO1','NAME1')),
    (Names: ('NO2','NAME2'))
  );

You would prefer to write the final declaration as

const
  NAMES1: TTwoNames = ('NO1','NAME1');
  NAMES2: TTwoNames = ('NO2','NAME2');

  StringArraysList: array[0..1] of TMyRec = (
    (Names: NAMES1),
    (Names: NAMES2)
  );

But that results in

[DCC Error] Unit1.pas(38): E2026 Constant expression expected

Some Delphi constants are not as constant as you would like them to be!

The documentation for record constants states that

The values must be represented by constant expressions.

The documentation for typed constants states that

Typed constants cannot occur in constant expressions.

Put these two rules together and we have E2026.

日裸衫吸2024-12-14 08:28:56

您可以声明一个新类型

TName = array[0..1] of string;

并在记录声明中使用它。
你的声明就变成了

type
  TName = array[0..1] of string;

  TMyRec = record
    Name: TName;
  end;

const
  StringArraysList: array[0..1] of TMyRec = (
    (Name: ('NO1','NAME1')),
    (Name: ('NO2','NAME2'))
  );

You could declare a new type

TName = array[0..1] of string;

and use that in your record declaration.
Your declaration then becomes

type
  TName = array[0..1] of string;

  TMyRec = record
    Name: TName;
  end;

const
  StringArraysList: array[0..1] of TMyRec = (
    (Name: ('NO1','NAME1')),
    (Name: ('NO2','NAME2'))
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文