I know how to convert an enumerated type to an integer.
type TMyType = (mtFirst, mtSecond, mtThird); var ordValue:integer; enumValue:TMyType; ... ordValue:= Ord(mtSecond); // result is 1
But how do I do the inverse operation and convert an integer to an enumerated type?
Use the IntEnum class from the enum module to convert an enum to an integer in Python. You can use the auto() class if the exact value is unimportant. To get a value of an enum member, use the value attribute on the member.
IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.
enumerations are of value type. They are created and stored on the stack and passed to the methods by making a copy of the value (by value). Enums are value type.
As Ken answered, you just cast it. But to make sure you have correct value you can use code like:
if (ordValue >= Ord(Low(TMyType))) and (ordValue <= Ord(High(TMyType))) then enunValue := TMyType(ordValue) else raise Exception.Create('ordValue out of TMyType range');
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