Delphi:如何拥有非连续子范围枚举类型?

发布于 2024-08-06 18:12:41 字数 286 浏览 10 评论 0原文

虽然以下子范围枚举声明有效:

type
   TReceiptCode = 'A'..'F';

这不起作用:

type
   TReceiptCode = ' ','A'..'F', 'R';

也不起作用

type
    TReceiptCode = ' ','A','B','C','D','E','F','R';

如何声明具有非连续值的子范围类型?

While the following subrange enumeration declaration works:

type
   TReceiptCode = 'A'..'F';

This does not:

type
   TReceiptCode = ' ','A'..'F', 'R';

Nor does

type
    TReceiptCode = ' ','A','B','C','D','E','F','R';

How can i declare a subrange type with non-contiguous values?

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

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

发布评论

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

评论(3

未央 2024-08-13 18:12:42

不幸的是,我认为没有任何办法可以做到这一点。您可以声明一个(新的)非连续枚举,或现有类型的子范围,但不能同时声明两者。

Unfortunately, I don't think there's any way to do that. You can declare a (new) non-contiguous enumeration, or a subrange of an existing type, but not both.

眉目亦如画i 2024-08-13 18:12:42

可以用一套代替吗?

TSomeCharSet= Set of Char;

SomeChars: TSomeCharSet = [' ','A','B','C','D','E','F','R'];

可能是奶奶和鸡蛋的情况,但我不确定你当时用的是什么:) ...

那么你剩下的就是使用 Set 或数组作为限制“范围”自己创建 TNonContigousCharRange 并在以下情况下引发异常它超出范围或有 SetReceiptCode 过程来执行类似的操作。

Could you use a set instead?

TSomeCharSet= Set of Char;

SomeChars: TSomeCharSet = [' ','A','B','C','D','E','F','R'];

Could be granny and egg situation but I'm not sure what you are using then for :) ...

Well all you are left with then is creating TNonContigousCharRange yourself using a Set or array as the limiting "Range" and raising an exception when it is out of range or having a SetReceiptCode procedure to do a similar thing.

终难愈 2024-08-13 18:12:42

对于之前的所有答案,我想简单地补充一点,线索就在类型的名称中: subrange

简而言之,范围有下限和上限。您所描述的是一个集合(或一个子集),而不是一个子范围,因此您当然不能将其表达为子范围。

To all previous answers I would add simply that the clue is in the name of the type: subrange

Simply put, a range has a lower and an upper bound. What you describe is a set (or a subset), not a subrange so of course you cannot express it as a subrange.

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