Delphi 中具有自定义值的枚举

发布于 2024-09-18 00:21:09 字数 136 浏览 8 评论 0原文

在 Delphi 5 中可以像这样声明带有自定义值的枚举吗?:

type
  MyEnum = (meVal1 = 1, meVal2 = 3); // compiler error

谢谢!

It is possible to declare enums with custom values in Delphi 5 like this?:

type
  MyEnum = (meVal1 = 1, meVal2 = 3); // compiler error

Thanks!

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

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

发布评论

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

评论(4

抠脚大汉 2024-09-25 00:21:10

如果您有旧版本的 Delphi (<= D5 IIRC),则无法执行此操作。也许你可以用常量替换枚举? 不幸的是

const
  meVal1 = 1;
  meVal2 = 3;

type
  TMyEnum = Byte; // or Integer or ... - depends on your needs.

,编译器无法像使用枚举类型那样为您执行尽可能多的错误检查。

If you have an older version of Delphi (<= D5 IIRC) you can't do this. Maybe you can replace the enum by constants? Something like

const
  meVal1 = 1;
  meVal2 = 3;

type
  TMyEnum = Byte; // or Integer or ... - depends on your needs.

Unfortunately, the compiler can't do as much error checking for you with this as with an enum type.

北城挽邺 2024-09-25 00:21:10

作为 Ulrich 答案的一个有点丑陋的扩展,你可以做如下的事情:

type
  TMyEnum = (meVal1, meVal2);

const
  MY_ENUM_VALS: array[TMyENum] of integer = (1, 3);

并以

if (aVal = MY_ENUM_VALS[meVal2]) then...

“不漂亮”的方式访问它们,我同意你,但至少这样你可以对那些早期版本的 Delphi 进行更多的编译器错误检查。

As a somewhat ugly extension to the answer by Ulrich you could do something like the following:

type
  TMyEnum = (meVal1, meVal2);

const
  MY_ENUM_VALS: array[TMyENum] of integer = (1, 3);

and access them as

if (aVal = MY_ENUM_VALS[meVal2]) then...

Not pretty, I grant you, but at least that way you get a little more compiler error checking for those earlier versions of Delphi.

终难遇 2024-09-25 00:21:09

在旧的德尔菲斯你可以做

type
  MyEnum = (meUnused1, meVal1, meUnused2, meVal2);

In older Delphis you can do

type
  MyEnum = (meUnused1, meVal1, meUnused2, meVal2);
柳若烟 2024-09-25 00:21:09

根据这篇文章,这是合法的。我确实记得在 Delphi 的早期版本中不支持提供值。

提供您收到的“编译器错误”可能会有所帮助。另外,你用的Delphi是什么版本?

This is legal according to this article. I do recall that in early versions of Delphi supplying values wasn't supported.

It might help to provide the 'compiler error' you received. Also, what version of Delphi are you using?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文