Thursday, April 2, 2009

Convert Integer To Enum Instance in C#

Enums are a powerful construction in C# and other programming languages when you are working with finite sets such as fruits, days of the week or colors. Visual Studio's Intellisense is very nice with Enums because it lists all the options for the programmer to choose from. And you can choose the enum which you required at any specific time of code.
For this post I have a EmployeeType enum type which has four named constants which are Manager, Grunt, Contractor and the VicePresident and i have set the integer value for the named constant which are 1, 2, 3 and 4 respectively. Suppose we have an integer value and we want to convert to the EmployeeType enum and then display the enum value in the form of sting to the user.
enum EmployeeType
{
Manager = 1,
Grunt = 2,
Contractor = 3,
VicePresident = 4,
}
To over come this problem of to convert from integer value to a give enum type C# has the Enum.ToObject method. Let me explain how it work and what parameter it will take. In the code below i have set the value of the integer to 3 and the variable which hold the value is "intValue" , The Enum.ToObject method takes two arguments. The first is the type of the enum you want to create as output. The second field is the int to convert. Obviously, there must be a corresponding Enum entry for the conversion to succeed.
int intValue = 3;
EmployeeType ConvertedValue = (EmployeeType)Enum.ToObject(typeof(EmployeeType), intValue);
Console.WriteLine(ConvertedValue.ToString());
Console.ReadKey();
As you can see from the above code that i have cast the return value of the Enum.ToObject function to my EmployeeType, because the Enum.ToObject function will return the System.Object type and you have to convert it to your enum type. Here is the another piece of code which will show you how you can check if the name constant value exist in the enum or not.
int intValue = 5;
EmployeeType ConvertedValue = (EmployeeType)Enum.ToObject(typeof(EmployeeType), intValue);
Boolean blnIsExist = Enum.IsDefined(typeof(EmployeeType), ConvertedValue);
if (blnIsExist)
{
Console.WriteLine("Value is define in named Constant");
Console.WriteLine(ConvertedValue.ToString());
}
else
Console.WriteLine("Not Found");
The above code is simple one, i have first convert the integer value to the EmployeeType enum and then in the next statement it will check whether the converted value exist in the EmployeeType enum or not by using the IsDefined function of the Enum class which will return true if the value exist in the given enum otherwise it will return false. Depending on the return value of the IsDefined function it will display message.In the above case it will display "Not Found" message as the integer value is 5.

All and any comments / bugs / suggestions are welcomed!


5 comments:

aj said...

can't we just use (EmployeeType)intValue

Asim Sajjad said...

aj,Yes you can convert it bye using your conversion. But to verify that the converted enum named constant belong to the EmployeeType, you have to use the Enum.IsDefined, as the conversion didn't give any error or exception if for example you convert the value 5 to EmployeeType.

aj said...

thanks asim for the clarification. can you please tell me the best way to convert an enum from string?

Asim Sajjad said...

aj, I am working on it "how to convert a string to enum Instant" you will see a post on it :)

aj said...

Thanks Asim. I really appreciate your efforts and quick help! :)