向枚举类型添加新元素

发布于 2024-12-02 01:51:02 字数 247 浏览 0 评论 0原文

给定 Delphi 中的枚举类型声明,例如:

TMyType = (Item1, Item2, Item3);

有没有办法在运行时将第四个项目(例如 Item4)添加到枚举类型,以便 在应用程序执行期间的某个时刻,我有:

TMyType = (Item1, Item2, Item3, Item4);

或者 Delphi 中的类型是固定的吗?

Given an enumerated type declaration in Delphi such as:

TMyType = (Item1, Item2, Item3);

is there any way to add a fourth item, say Item4, to the enumerate type at runtime so that
at some point during the application's execution I have:

TMyType = (Item1, Item2, Item3, Item4);

Or are types fixed in Delphi?

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

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

发布评论

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

评论(4

夜声 2024-12-09 01:51:02

您可以创建一个

以下示例使用项目 item1item4 初始化该集。
之后添加 item5
它显示了添加之前和之后的 item5 是否在集合中,因此您将得到以下输出:

FALSE
TRUE

示例:

program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;

type
  TMyType = (Item1, Item2, Item3, Item4, Item5,Item6);
const
  cItems1And4 : set of TMyType = [Item1,Item4];
var
  MyItems:set of TMyType;

begin
  MyItems := cItems1And4;
  WriteLn(Item5 in MyItems);
  Include(MyItems,Item5);
  WriteLn(Item5 in MyItems);
  ReadLn;
end.

...

我想输入以下内容作为对 Andreases 回复的评论,但评论系统不允许我正确输入格式的东西..

如果你不坚持使用古老的Delphi,这可能是一个更好的主意:

  TComputerType = record
    const
      Desktop = 0;
      Server = 1;
      Netbook = 2;
      Tabled = 3;
  end;

这确保你不会污染你的命名空间,并且你可以像使用作用域枚举一样使用它:

 TComputerType.Netbook

我通常现在就这样做..你甚至可以在那里创建一些方便的辅助函数或属性,例如从字符串转换到字符串。

You can create a set.

The following example initializes the set with items item1 and item4.
After that item5 is added.
It shows whether item5 is in the set, before and after adding, so you'll get this output:

FALSE
TRUE

Example:

program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;

type
  TMyType = (Item1, Item2, Item3, Item4, Item5,Item6);
const
  cItems1And4 : set of TMyType = [Item1,Item4];
var
  MyItems:set of TMyType;

begin
  MyItems := cItems1And4;
  WriteLn(Item5 in MyItems);
  Include(MyItems,Item5);
  WriteLn(Item5 in MyItems);
  ReadLn;
end.

...

I wanted to type the following as a comment to Andreases reply, but the comment system doesn't let me properly format stuff..

If you're not stuck with an ancient Delphi, this is probably a better idea:

  TComputerType = record
    const
      Desktop = 0;
      Server = 1;
      Netbook = 2;
      Tabled = 3;
  end;

That makes sure you don't pollute your namespace, and you'll get to use it as if it's a scoped enum:

 TComputerType.Netbook

I usually do it like that nowadays.. You can even create some handy helper-functions or properties in there, for example to convert from and to strings.

国际总奸 2024-12-09 01:51:02

不,你“不能”这样做。这违背了 Delphi 的工作方式。 (回想一下,Delphi 在编译时就已经检查了您的类型。)

如果我是您,我不会这样做,

TComputerType = (ctDesktop, ctServer, ctLaptop, ctNetbook, ctTablet)

相反,我会这样做,

TComputerType = integer;

const
  COMPUTER_TYPE_DESKTOP = 0;
  COMPUTER_TYPE_SERVER = 1;
  COMPUTER_TYPE_LAPTOP = 2;
  COMPUTER_TYPE_NETBOOK = 3;
  COMPUTER_TYPE_TABLET = 4;

我相信您明白原因。

No, you 'can't' do this. It is against the way Delphi works. (Recall that Delphi checks your types already at compile time.)

If I were you, I'd not do

TComputerType = (ctDesktop, ctServer, ctLaptop, ctNetbook, ctTablet)

Instead, I'd do

TComputerType = integer;

const
  COMPUTER_TYPE_DESKTOP = 0;
  COMPUTER_TYPE_SERVER = 1;
  COMPUTER_TYPE_LAPTOP = 2;
  COMPUTER_TYPE_NETBOOK = 3;
  COMPUTER_TYPE_TABLET = 4;

I am sure you get why.

还给你自由 2024-12-09 01:51:02

在 Delphi 中,类型在编译时就已确定——毕竟,它是一种静态类型语言。

您可以在编译时定义枚举的子范围

type
  TEnum = (item1, item2, item3, item4);
  TSubRangeEnum = item1..item3;

Types are fixed at compile time in Delphi—it is, after all, a statically typed language.

You can, at compile time, define subranges of an enumeration:

type
  TEnum = (item1, item2, item3, item4);
  TSubRangeEnum = item1..item3;
狼性发作 2024-12-09 01:51:02

由于我还无法发表评论,因此我将在 Andreas 建议的内容中添加一份附录,如果您有许多此类列表需要维护,这可能会有所帮助。为每个分组添加一个“基”常量可能有助于在代码中更好地组织它们,并有助于稍后调试常量(当然,假设每个组都有一个唯一的基)。

TComputerType = integer;

const
  COMPUTER_TYPE_BASE = 100;
  COMPUTER_TYPE_DESKTOP = COMPUTER_TYPE_BASE + 1;
  COMPUTER_TYPE_SERVER = COMPUTER_TYPE_BASE + 2;
  COMPUTER_TYPE_LAPTOP = COMPUTER_TYPE_BASE + 3;
  COMPUTER_TYPE_NETBOOK = COMPUTER_TYPE_BASE + 4;
  COMPUTER_TYPE_TABLET = COMPUTER_TYPE_BASE + 5;

Since I cannot yet comment, I will add one addendum to what Andreas suggested that might help if you have many such lists to maintain. Adding a "base" constant for each grouping might help keep them better organized within your code and help with debugging the constants later (assuming each group has a unique base, of course).

TComputerType = integer;

const
  COMPUTER_TYPE_BASE = 100;
  COMPUTER_TYPE_DESKTOP = COMPUTER_TYPE_BASE + 1;
  COMPUTER_TYPE_SERVER = COMPUTER_TYPE_BASE + 2;
  COMPUTER_TYPE_LAPTOP = COMPUTER_TYPE_BASE + 3;
  COMPUTER_TYPE_NETBOOK = COMPUTER_TYPE_BASE + 4;
  COMPUTER_TYPE_TABLET = COMPUTER_TYPE_BASE + 5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文