简体   繁体   English

将新元素添加到枚举类型

[英]Adding new elements to an enumerated type

Given an enumerated type declaration in Delphi such as: 给定Delphi中的枚举类型声明,例如:

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: 有没有办法在运行时将第四项(比如Item4)添加到枚举类型中,以便在应用程序执行期间的某个时刻我有:

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

Or are types fixed in Delphi? 或者是在Delphi中修复的类型?

You can create a set . 您可以创建一个set

The following example initializes the set with items item1 and item4 . 以下示例使用item1item4项初始化集。 After that item5 is added. 之后添加item5 It shows whether item5 is in the set, before and after adding, so you'll get this output: 它显示item5是否在添加之前和之后的集合中,因此您将获得此输出:

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.. 我想输入以下内容作为对Andreases回复的评论,但评论系统不允许我正确格式化东西..

If you're not stuck with an ancient Delphi, this is probably a better idea: 如果你没有坚持使用古老的Delphi,这可能是一个更好的主意:

  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. 我现在通常这样做..你甚至可以在那里创建一些方便的辅助函数或属性,例如转换为字符串和转换为字符串。

No, you 'can't' do this. 不,你'不能'做到这一点。 It is against the way Delphi works. 这违反了Delphi的工作方式。 (Recall that Delphi checks your types already at compile time.) (回想一下Delphi在编译时已经检查过你的类型。)

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. 我相信你明白了。

Types are fixed at compile time in Delphi—it is, after all, a statically typed language. 类型在Delphi的编译时是固定的 - 毕竟它是一种静态类型的语言。

You can, at compile time, define subranges of an enumeration: 您可以在编译时定义枚举的子范围

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

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM