Geeks With Blogs
Tim Huffam Dotting the I and crossing the T of I.T.

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 C# .NET , ASP.NET | Back to top


Comments on this post: Using strings and enum - C# .NET

# re: Using strings and enum - C# .NET
Requesting Gravatar...
Thanks a lot. It saved me a lot of time.
Left by Hasib Muyen on Feb 11, 2008 3:37 AM

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

# re: Using strings and enum - C# .NET
Requesting Gravatar...
can i ask some help??
Left by therese on Feb 16, 2008 3:45 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
your a geek but make enums with CAPS
Left by asdf on Feb 18, 2008 6:27 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
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.
Left by CMG on Feb 29, 2008 5:50 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
hi
this is very good comments
i am very thankingful to you









Left by mukesh on Jul 14, 2008 5:49 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...

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.

Left by Mark on Sep 29, 2008 9:15 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
Good work
Left by Peter on Jan 22, 2009 5:24 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
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;
}
}
Left by Cory on Feb 25, 2009 7:05 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
hey good topic
Thanks :)
Left by anollipian on May 16, 2009 7:28 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
gr8 article !!!
Left by Kris29 on May 31, 2009 4:34 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
thanks man
can i know hot to convert from enum items to int ?
Left by almny on Jun 04, 2009 12:02 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
Nice job Cory. Tim - I would spend a bit more time looking at problems people are having and the reasoning behind it.
Left by anon on Jul 02, 2009 12:01 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
Take it up the arse you dog fucker
Left by CockFucker on Aug 13, 2009 12:02 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
good one!! apt solution for my problem
Left by Developer on Sep 11, 2009 12:39 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
Great overview.. very clean solution to my problem
Left by Damian on Oct 26, 2009 4:01 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
I like Big Butts
Left by Rawr on Nov 20, 2009 4:13 PM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
thanks for sharing.
http://www.tashaapparel.com
Left by wholesale clothing on Jan 06, 2010 11:40 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
This is as Like as i NeeD..........
Left by Amin on Feb 09, 2010 3:24 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
Thank you for your article, very readable, and I long exposure.
Left by Wholesale Polo Ralph Lauren on Apr 06, 2011 11:44 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
this is very use full for me
thankssssssssssssssss
Left by mahesh babu ummaneni on Jul 12, 2011 9:09 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
check out this link i hope it has some useful stuff
http://msdotnetbuddy.blogspot.in/2010/06/enum-in-c.html
Left by sangram on Mar 10, 2012 1:49 AM

# re: Using strings and enum - C# .NET
Requesting Gravatar...
this is very usefull for me.,
Left by cva on Aug 10, 2012 7:44 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net