Converting Enumerations

You can converting enumerations to other values and vice versa. Enumeration values are actually numeric values that are just masked by their names so that they can easily be understood. Let’s take a look at an example.

using System;

namespace ConvertingEnumerationsDemo
{
    enum Direction
    {
        North,
        East,
        South,
        West
    }
    public class Program
    {
        public static void Main()
        {
            Direction myDirection = Direction.East;
            int myDirectionCode = (int)myDirection;


            Console.WriteLine("Value of East is {0}", myDirectionCode);

            myDirection = (Direction)3;
            Console.WriteLine("\nDirection: {0}", myDirection.ToString());
        }
    }
}

Example 1

Value of East is 1

Direction: West

In line 16, we assigned the variable myDirection with the value East from the Direction enumeration. With the default values for the items in the enumeration, East should have a value of 1.  Line 17 shows you how to convert an enumeration item into its equivalent integer value  using casting. The syntax is as follows.

variable = (DestinationDataType)enumerationVariable;

Since myDirectionCode variable is of type int, it is expecting an int to be its value. We simply put the destination type inside a set of parentheses  and put that group besides the enumeration variable. The result returned is the converted value.

In line 21, we do the reverse. We convert an integer number into an enumeration value. The value 3 equates to the enumeration item West. To convert that, we used the same syntax of          converting a Direction to an integer. Take note that if the number you will be converting is not in the bounds of the enumeration, it will still be converted but no equivalent enumeration item will be assigned for it. For example:

myDirection = (Direction)10;

Console.WriteLine("Direction: {0}", myDirection.ToString());
 Direction: 10

Since 10 is not a value of any of the enumeration items, the resulting output in          the console would be the number itself.

Converting a String into an Enumeration Value

You can convert a string into an enumeration value. Say for example you have a string “West” and you want to convert it to Direction.West, then you have to do a more complex technique. You need to use the Enum class of the System namespace of .NET Framework.

Direction myDirection = (Direction)Enum.Parse(typeof(Direction), "West");

Console.WriteLine("Direction: {0}", myDirection.ToString());
Direction: West

The Enum.Parse() method has two parameters. The first is the type of the enumeration. We used the typeof operator to return the type of the enumeration.The second parameter is the  string to be converted to the enumeration type provided in the first argument.  The value returned is of type object so a conversion to the proper enumeration type is necessary. With those details, we can now make a syntax for converting a string into an enumeration item.

enumType name = (enumType)Enum.Parse(typeof(enumType), string);

If the string you passed to the method cannot be mapped or is not a member of the enumeration, then an error will occur.