Is it possible to cast/convert an Enumeration Value to Integer in Delphi?
If yes, then how?
This is called out explicitly at the documentation for enumerated types:
Several predefined functions operate on ordinal values and type identifiers. The most important of them are summarized below.
| 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. |
I see David has posted you a good answer while I was writing this, but I'll post it anyway:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With