遍历Delphi中的不规则枚举

遍历Delphi中的不规则枚举

本文介绍了遍历Delphi中的不规则枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)有人知道是否有可能遍历Delphi(XE)中的不规则枚举吗?

1) Does anyone know if it is possible to loop through an irregular enumeration in Delphi (XE)?

可以正常枚举.来自 Delphi基础:

var
  suit : (Hearts, Clubs, Diamonds, Spades);
begin
// Loop 3 times
For suit := Hearts to Diamonds do
   ShowMessage('Suit = '+IntToStr(Ord(suit)));
end;

但是,如果将'suit'声明为

But, if 'suit' instead is declared as

var
  suit : (Hearts=1, Clubs, Diamonds=10, Spades);

它循环10次.不令人惊讶,但我想循环3.到目前为止,我发现的唯一解决方案是将枚举转换为集合,并像在 delphi.about.com .

it loops 10 times. Not suprising, but I would like to loop 3. The only solution I've found so far is converting an enumeration to a set and use the 'for ... in'-loop like on delphi.about.com.

因此,如果对问题1)的回答为否",则:
2)如何从枚举转换为Delphi中的设置?

So, if answer to question 1) is no, then:
2) How to convert from enumeration to set in Delphi?

我在其中使用的上下文是具有不规则编号(edit1,edit5,edit7,edit3,...)的编辑框(TEdit)的组件数组.尽管可以对所有编辑框重新排序,但它消除了使用枚举作为允许在枚举中间添加编辑框的灵活方法的原因.

The context I am using it in is a component array of edit-boxes (TEdit) that has an irregular numbering (edit1, edit5, edit7, edit3, ...). While it is possible to reorder all the edit-boxes, it removes the reason of using enumeration as a flexible way to allow addition of an edit-box in the middle of the enumeration.

推荐答案

我现在没有Delphi编译器,但是我认为通过这样做可以大大改善gabr的方法

I do not have a Delphi compiler at hand right now, but I tink that gabr's approach can be rather significantly improved by doing

type
  TSuit = (Hearts = 1, Clubs, Diamonds = 10, Spades);

const
  Suits: array[0..3] of TSuit = (Hearts, Clubs, Diamonds, Spades);

谁知道,也许它甚至不编译.

Who knows, maybe it doesn't even compile.

这篇关于遍历Delphi中的不规则枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:45