Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an integer to an enumerated type?

Tags:

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?

like image 428
lyborko Avatar asked Jan 04 '12 19:01

lyborko


People also ask

How do you convert enum to int in Python?

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.

Can you convert string to enum?

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.

Is enumeration a value type?

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.


1 Answers

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'); 
like image 65
ain Avatar answered Nov 06 '22 00:11

ain