# 枚举

//定义
type
Colors = (Red, Yellow, Green, Cyan, Blue, Violet);
Suit = (Club, Diamond, Heart, Spade);

//举例：
type
Colors = (Red, Yellow, Green, Cyan, Blue, Violet);
var
C: Colors;
begin
C := Red;
ShowMessage(IntToStr(Ord(C)));  //0
C := Blue;
ShowMessage(IntToStr(Ord(C)));  //4

C := Green;
ShowMessage(IntToStr(Integer(C)));  //2; 获取元素位置用 Integer 也可以
end;

//举例2：
type
Tabc = (a=5,b=10,c=a+b);
var
ta,tb,tc: Tabc;
begin
ta := a;
tb := b;
tc := c;
ShowMessage(IntToStr(Ord(ta)));  //5
ShowMessage(IntToStr(Ord(tb)));  //10
ShowMessage(IntToStr(Ord(tc)));  //15
end;

//获取枚举元素总数：
type
TMyEnum = (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday);
var
m: TMyEnum;
num: Integer;
begin
num := 0;
for m := Low(TMyEnum) to High(TMyEnum) do
Inc(num);

ShowMessage(IntToStr(num));  // 7
end;

//返回枚举元素的字符串标识：
uses TypInfo;  //需要uses的单元
type
TMyEnum = (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday);

procedure TForm1.Button1Click(Sender: TObject);
var
str: string;
begin
str := TypInfo.GetEnumName(TypeInfo(TMyEnum),1);
ShowMessage(str);  //Tuesday
end;

//获取对象枚举值的字符串表式：
uses TypInfo;  //需要uses的单元

procedure TForm1.FormCreate(Sender: TObject);
var
str: string;
begin
str := GetEnumProp(Self, 'BorderStyle');
ShowMessage(str); { bsSizeable }
end;


posted on 2007-11-30 18:00  万一  阅读(...)  评论(...编辑  收藏