Earlier, we posted a tutorial on how to get an enum from a number. In this tutorial we're going to show you how to use a similar technique to get an enum from a string.
Just like last time I'm going to create a generic method for converting the string to an enum. This lets us convert to any type of enum using a single method.
public static T StringToEnum<T>(string name)
{
return (T)Enum.Parse(typeof(T), name);
}
The code to actually get an enum from a string is very simple thanks to .NET's Enum class. All we have to do is call Parse, passing in the type of enum and the string. Since Parse returns an object, we need to cast it to our desired type.
Let's see how to use this function using some examples.
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
public enum MonthsInYear
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
DaysOfWeek d = StringToEnum<DaysOfWeek>("Monday");
//d is now DaysOfWeek.Monday
MonthsInYear m = StringToEnum<MonthsInYear>("January");
//m is now MonthsInYear.January
So what happens if you enter a string value that doesn't correspond to an enum? The Enum.Parse will fail with an ArgumentException.
//throws an ArgumentException
//Requested value "Katillsday" was not found.
We can get around this problem by first checking that the enum exists using Enum.IsDefined.
StringToEnum<DaysOfWeek>("Katillsday");
That about does it for converting a string to a enum using .NET and C#. If you have any comments or questions, let us know.
11/28/2007 - 11:21
thanks mate... this function has been stumping me for a week now, it's been driving me utterly batty!
01/30/2008 - 07:56
Cross linked ya!
05/14/2008 - 23:28
好!(Good)
06/23/2008 - 01:54
I need this code.
Can anyone Please tell the code code in vb.net
my mail id is : aswinandaswin@gmail.com
03/28/2009 - 12:08
lol)))) VB sucks)))
actualy use Enum.Parse(typeof T , name)
06/17/2009 - 20:45
could you please provide me this code in VC++
06/17/2009 - 20:46
i mean to say VC++.NET
Thanks in Adavance
06/18/2009 - 06:49
Generics in VC++ are a little strange. Here's the function declaration and body.
static T StringToEnum(System::String^ name);
T MyClass::StringToEnum(System::String^ name)
{
return (T)System::Enum::Parse(T::typeid, name);
}
And here's how to call it:
MyClass::StringToEnum<System::DayOfWeek>("Wednesday");
//day is now System::DayOfWeek::Wednesday
09/18/2009 - 19:56
This totally rocks!! I had been beating my head against a wall for a solution to programatically setting a 3rd party utility property that uses enums. This is a simple and graceful solution.
Thank You for sharing!!
12/14/2009 - 14:36
I like to go thru my dad's sock drawer looking for spare change cuz he works late.
12/14/2009 - 14:38
This is by far the most on-topic, thought-inspiring, comment I've ever had the privilege to read during my three years of contributing to this blog. Well done sir, well done.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.