Tim Huffam

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

  Home  |   Contact  |   Syndication    |   Login
  152 Posts | 0 Stories | 2310 Comments | 653 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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Tuesday, September 18, 2007 3:47 AM

Feedback

# 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 7/14/2008 5:49 PM mukesh
hi
this is very good comments
i am very thankingful to you











# re: Using strings and enum - C# .NET 9/29/2008 9:15 AM Mark

Obviously you are not aware of the reason the authors of the articles you have been reading are proposing these "convoluted" methods. It is because "out of the box", .NET enums do not support string values, and thereby, also do not support getting the enum element from a string value.

For example, you cannot do

public enum MessageType
{
ADMINISTRATIVE = "AD",
INQURIY = "IQ"
}

In this example, you cannot override the toString() method with anything out of the box. And you cannot get the ADMINISTRATIVE element from the string value "AD" with any out of the box capability.

Functionality like this is often necessary in the situations in which you would like to use enums, and since this functionality is not possible out of the box, some authors have preseneted their work arounds to achieve this functionality.



# re: Using strings and enum - C# .NET 1/22/2009 5:24 AM Peter
Good work

# re: Using strings and enum - C# .NET 2/25/2009 7:05 AM Cory
I know this post is from a while back but with .NET 3.5 this is possible (but sorta gross) through extension methods:

public enum LoggingLevel
{
Debug,
Error,
Warning,
Information
}

public static class ExtensionMethods
{
public static string ToString(this LoggingLevel loggingLevel)
{
string retVal = string.Empty;
switch (loggingLevel)
{
case LoggingLevel.Debug:
{
retVal = "*D*";
break;
}
case LoggingLevel.Error:
{
retVal = "*E*";
break;
}
case LoggingLevel.Information:
{
retVal = "*I*";
break;
}
case LoggingLevel.Warning:
{
retVal = "*W*";
break;
}
}
return retVal;
}
}

# re: Using strings and enum - C# .NET 5/16/2009 7:28 PM anollipian
hey good topic
Thanks :)

# re: Using strings and enum - C# .NET 5/31/2009 4:34 AM Kris29
gr8 article !!!

# re: Using strings and enum - C# .NET 6/4/2009 12:02 AM almny
thanks man
can i know hot to convert from enum items to int ?

# re: Using strings and enum - C# .NET 7/2/2009 12:01 PM anon
Nice job Cory. Tim - I would spend a bit more time looking at problems people are having and the reasoning behind it.

# re: Using strings and enum - C# .NET 8/13/2009 12:02 AM CockFucker
Take it up the arse you dog fucker

# re: Using strings and enum - C# .NET 9/11/2009 12:39 PM Developer
good one!! apt solution for my problem

# re: Using strings and enum - C# .NET 10/26/2009 4:01 PM Damian
Great overview.. very clean solution to my problem

# re: Using strings and enum - C# .NET 11/20/2009 4:13 PM Rawr
I like Big Butts

# re: Using strings and enum - C# .NET 1/6/2010 11:40 AM wholesale clothing
thanks for sharing.
http://www.tashaapparel.com

# re: Using strings and enum - C# .NET 2/9/2010 3:24 AM Amin
This is as Like as i NeeD..........

# re: Using strings and enum - C# .NET 4/6/2011 11:44 AM Wholesale Polo Ralph Lauren
Thank you for your article, very readable, and I long exposure.

# re: Using strings and enum - C# .NET 7/12/2011 9:09 AM mahesh babu ummaneni
this is very use full for me
thankssssssssssssssss

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: