将枚举值转换为 Delphi 中的整数
Cast Enumeration Value to Integer in Delphi
是否可以将cast/convert枚举值Delphi中的整数?
如果是,那又如何?
您可以为此使用 Ord() 函数。为了清楚起见,最好编写一对 IntToEnum() 和 EnumToInt() 函数。
这在 documentation for enumerated types:
处明确调用
几个预定义函数对序数值和类型标识符进行操作。其中最重要的总结如下。
| Function | Parameter | Return value | Remarks |
|----------|:-----------------------------------------------------:|----------------------------------:|---------------------------------------------------|
| Ord | Ordinal expression | Ordinality of expression's value | Does not take Int64 arguments. |
| Pred | Ordinal expression | Predecessor of expression's value | |
| Succ | Ordinal expression | Successor of expression's value | |
| High | Ordinal type identifier or variable of ordinal type | Highest value in type | Also operates on short-string types and arrays. |
| Low | Ordinal type identifier or variable of ordinal type | Lowest value in type | Also operates on short-string types and arrays. |
我看到大卫 post 在我写这篇文章的时候给了你一个很好的答案,但我还是 post 了:
program enums;
{$APPTYPE CONSOLE}
uses
SysUtils, typinfo;
type
TMyEnum = (One, Two, Three);
var
MyEnum : TMyEnum;
begin
MyEnum := Two;
writeln(Ord(MyEnum)); // writes 1, because first element in enumeration is numbered zero
MyEnum := TMyEnum(2); // Use TMyEnum as if it were a function
Writeln (GetEnumName(TypeInfo(TMyEnum), Ord(MyEnum))); // Use RTTI to return the enum value's name
readln;
end.
将枚举转换为整数有效。我无法评论其他答案,因此将其发布为答案。转换为整数可能不是一个好主意(如果是请评论)。
type
TMyEnum = (zero, one, two);
var
i: integer;
begin
i := integer(two); // convert enum item to integer
showmessage(inttostr(i)); // prints 2
end;
这可能类似于 Ord(),但我不确定哪个是最佳实践。如果您将枚举转换为整数
,以上内容也适用
type
TMyEnum = (zero, one, two);
var
MyEnum: TMyEnum;
i: integer;
begin
MyEnum := two;
i := integer(MyEnum); // convert enum to integer
showmessage(inttostr(i)); // prints 2
end;
是否可以将cast/convert枚举值Delphi中的整数?
如果是,那又如何?
您可以为此使用 Ord() 函数。为了清楚起见,最好编写一对 IntToEnum() 和 EnumToInt() 函数。
这在 documentation for enumerated types:
处明确调用几个预定义函数对序数值和类型标识符进行操作。其中最重要的总结如下。
| Function | Parameter | Return value | Remarks | |----------|:-----------------------------------------------------:|----------------------------------:|---------------------------------------------------| | Ord | Ordinal expression | Ordinality of expression's value | Does not take Int64 arguments. | | Pred | Ordinal expression | Predecessor of expression's value | | | Succ | Ordinal expression | Successor of expression's value | | | High | Ordinal type identifier or variable of ordinal type | Highest value in type | Also operates on short-string types and arrays. | | Low | Ordinal type identifier or variable of ordinal type | Lowest value in type | Also operates on short-string types and arrays. |
我看到大卫 post 在我写这篇文章的时候给了你一个很好的答案,但我还是 post 了:
program enums;
{$APPTYPE CONSOLE}
uses
SysUtils, typinfo;
type
TMyEnum = (One, Two, Three);
var
MyEnum : TMyEnum;
begin
MyEnum := Two;
writeln(Ord(MyEnum)); // writes 1, because first element in enumeration is numbered zero
MyEnum := TMyEnum(2); // Use TMyEnum as if it were a function
Writeln (GetEnumName(TypeInfo(TMyEnum), Ord(MyEnum))); // Use RTTI to return the enum value's name
readln;
end.
将枚举转换为整数有效。我无法评论其他答案,因此将其发布为答案。转换为整数可能不是一个好主意(如果是请评论)。
type
TMyEnum = (zero, one, two);
var
i: integer;
begin
i := integer(two); // convert enum item to integer
showmessage(inttostr(i)); // prints 2
end;
这可能类似于 Ord(),但我不确定哪个是最佳实践。如果您将枚举转换为整数
,以上内容也适用type
TMyEnum = (zero, one, two);
var
MyEnum: TMyEnum;
i: integer;
begin
MyEnum := two;
i := integer(MyEnum); // convert enum to integer
showmessage(inttostr(i)); // prints 2
end;