Vadim's Weblog

Never stop learning.

Enum Conversion.

Posted by Vadim on October 19, 2007

In this post I will demonstrate how to convert Enum To String, String To Enum, and Value to Enum.

First of all let us create an enumeration:

   1: enum Colors
   2: {
   3:     Blue = 1, 
   4:     Green = 2, 
   5:     Red = 4
   6: }

 

Convert Enum To String.

It seams that nothing can be easier.  We easily can use ToString() method to convert Enum to String.  It would work fine if we try to convert Colors.Green, Colors.Blue, or Color.Red.  However, what if we try to convert to String value like this:

   1: Colors myColor = Colors.Green | Colors.Blue;
   2: Console.WriteLine(myColor.ToString());

The output will be ‘3’ instead of ‘Blue, Green’.

There are two ways we can solve this problem:

We can just add FlagsAttribute to our enumeration.

   1: [FlagsAttribute]
   2: enum Colors
   3: {
   4:     Blue = 1, 
   5:     Green = 2, 
   6:     Red = 4
   7: }

Or another solution we can use Format() or ToString() methods with format “F” or “f”.

   1: Colors myColor = Colors.Green | Colors.Blue;
   2: Console.WriteLine(Enum.Format(typeof(Colors), myColor, "F"));
   3: Console.WriteLine(myColor.ToString("f"));

In case you decorated your enumertion with FlagsAttribute but want to output the value, use Format() or ToString() methods with format “D” for decimal form or “X” for hexadecimal one.

   1: Colors myColor = Colors.Green | Colors.Blue;
   2: // Format()
   3: Console.WriteLine(Enum.Format(typeof(Colors), myColor, "d"));
   4: Console.WriteLine(Enum.Format(typeof(Colors), myColor, "x"));
   5: // ToString()
   6: Console.WriteLine(myColor.ToString("D"));
   7: Console.WriteLine(myColor.ToString("X"));

The method with format “D” will output ‘3’ and with format “X” will give us ‘00000003’.

 

Convert String To Enum.

To convert String to Enum we just need to use Parse() method.

   1: public Colors String2Enum(string colorString)
   2: {
   3:     return (Colors)Enum.Parse(typeof(Colors), colorString);
   4: }

In the example above colorString must match case exactly.  There’s an override for Parse() method where you can ask to ignore case.

   1: public Colors String2Enum(string colorString)
   2: {
   3:     return (Colors)Enum.Parse(typeof(Colors), colorString, true);
   4: }

 

Convert Value To Enum.

We will use ToObject() method to convert value to Enum.

   1: public Colors Value2Enum(int colorValue)
   2: {
   3:     return (Colors)Enum.ToObject(typeof(Colors), colorValue);
   4: }

Update: Thanks to Omer Mor (see his comment below) who pointed out that we don’t have to invoke ToObject() method.  We can simply cast value to Enum type.

   1: public Colors Value2Enum(int colorValue)
   2: {
   3:     return (Colors) colorValue;
   4: }

kick it on DotNetKicks.com

4 Responses to “Enum Conversion.”

  1. Omer Mor said

    To convert a value to enum, I believe a simple explicit cast will work:

    public Colors Value2Enum(int colorValue)
    {
    return (Colors)colorValue;
    }

  2. vkreynin said

    Omer,

    Thanks for the comment. It’s not just simpler it’s also more efficient way. When you use ToObject, you’ll have an extra unboxing.

    I can’t believe that I didn’t put it in my blog originally because when I have to deal with Enum types in my code, I don’t call ToObject, I use simple casting just like you suggested.

  3. Rado said

    Hey Vadim thanks. Great samples

  4. Marko Erzen said

    it also works Cint(Color.Blue).ToString (VB sysntax)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: