Sha*_*elt 4 delphi pascal integer enumeration
是否可以在Delphi中将枚举值转换/转换为Integer?
如果是,那怎么样?
Dav*_*nan 19
几个预定义函数对序数值和类型标识符进行操作.其中最重要的概述如下.
| 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. |
在我写这篇文章时,我看到大卫给你发了一个很好的答案,但无论如何我都会发布它:
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.
Run Code Online (Sandbox Code Playgroud)