Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  118 Posts | 0 Stories | 640 Comments | 677 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

I've read a few articles recently regarding the use of strings with enums - discussing how to access the constant name values or how to set a variable with the correct value when you only have a string value (enum constant name) - and all present somewhat convoluted ways of achieving this. 

I'm guessing the authors are not aware that the .Net framework provides methods for this - out the box.

Hopefully the following code shows how to do this.

private enum CarTypes
{
  Lotus = 0,
  Morgan = 1,
  Atom = 2
}

private void button1_Click(object sender, EventArgs e)
{
  CarTypes myCarType = CarTypes.Morgan;
  textBox1.Text = Enum.GetName(typeof(CarTypes), myCarType) + ", " + myCarType.ToString() + ", " + (myCarType==CarTypes.Morgan).ToString();
  
 
myCarType = (CarTypes)Enum.Parse(typeof(CarTypes), "Atom");
 
textBox1.Text += "\r\n" + Enum.GetName(typeof(CarTypes), myCarType) + ", " + myCarType.ToString() + ", " + (myCarType == CarTypes.Atom).ToString();

 
myCarType = (CarTypes)Enum.Parse(typeof(CarTypes), "loTus", true);
 
textBox1.Text += "\r\n" + Enum.GetName(typeof(CarTypes), myCarType) + ", " + myCarType.ToString() + ", " + (myCarType == CarTypes.Lotus).ToString();
}

Output:

Morgan, Morgan, True
Atom, Atom, True
Lotus, Lotus, True

*** Update ***

For those wanting to know how to get the enum type based on the integer value,  you can simply cast it eg:

int carTypeInteger = 1;
CarTypes carType = (CarTypes)carTypeInteger;

carType would be set to Morgan

and visa-versa:
int carTypeInteger = (int)CarTypes.Morgan;

carTypeInteger would be set to 1

Simple stuff
Tim

posted on Tuesday, September 18, 2007 3:47 AM

Feedback

# re: Using strings and enum - C# .NET 9/18/2007 12:00 PM Mike
Can only guess at the articles you have read as you don't link to them, but the couple I've seen recently were trying to put characters such as spaces in the description, which you can't do "out of the box".

# re: Using strings and enum - C# .NET 2/11/2008 3:37 AM Hasib Muyen
Thanks a lot. It saved me a lot of time.

# re: Using strings and enum - C# .NET 2/14/2008 2:40 PM Aaron
Thanks for this, its exactly what I was after! Good work.

# re: Using strings and enum - C# .NET 2/16/2008 3:45 AM therese
can i ask some help??

# re: Using strings and enum - C# .NET 2/18/2008 6:27 AM asdf
your a geek but make enums with CAPS

# re: Using strings and enum - C# .NET 2/29/2008 5:50 PM CMG
How about a more practical example that demonstrates resolving the numeric enum value.
Instead of this:
CarTypes myCarType = CarTypes.Morgan;
Declare as:
CarTypes myCarType = 1;
... which in a real application would come from raw data.

# re: Using strings and enum - C# .NET 5/10/2008 6:26 AM Thanks
Thanks for setting the variable using string values.

# re: Using strings and enum - C# .NET 7/14/2008 5:49 PM mukesh
hi
this is very good comments
i am very thankingful to you











Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 2 and 8 and type the answer here: