Mark Wagner's .NET C# Cogitation

.NET Architect and Developer - Simple Thoughts from a Simple Mind

  Home :: Contact :: Syndication  :: Login
  90 Posts :: 59 Articles :: 1548 Comments :: 187 Trackbacks

News

You can download this .TEXT blog skin free. Download Cogitation Blue skin.
Updated May 6, 2004

My Sites
- New SharePoint Blog
- My Developer blog
- My Personal blog
ASP.NET Web Hosting
I host my site here. The best value for quality hosting. Read my opinion here. If you decide to join, please use this link. Thank you very much.


Legal
Any and all code, software, examples, suggestions and anything else on this web site is available for you to use at your own risk. No warranty is express or implied.
Views and Opinions
Mark Wagner works for Microsoft in the Consulting Services division and covers the West Region. The views and opinions expressed on this web site are not necessarily the views or opinions of his employer.

Article Categories

Archives

Post Categories

.: Architecture Links :.

.: Developer Links :.

.Text and RSS Links

Blog Roll - Top Guns

Developer: .NET Security

Developer: C# Team

Developer: Flash

Developer: JavaScript

Developer: Pocket PC

Personal: Aviation

Personal: Favorites

Convert a string to an enumerated (enum) value.

Using the Enum.Parse method, you can easily convert a string value to an enumerated value.  Doing this requires the type of the enum and string value.  Adding the true argument will cause the case to be ignored.

Using the following enum for this example:

private enum Aircraft
{
   Beech,
   Cessna,
   Piper
}

You can easily convert the string to an enum value like this:

Aircraft air = (Aircraft) Enum.Parse(typeof(Aircraft), "Cessna", true);

Ideally you should wrap a try-catch around the Enum.Parse statement.

posted on Thursday, April 07, 2005 8:12 AM

Feedback

# re: Convert a string to an enumerated (enum) value 4/7/2005 9:07 AM Chris Miller
I didn't know you could do that. I'm porting a SQL WHERE clause generator from Delphi to C# and that technique will clean up some ugly code that I just wrote.

# re: Convert a string to an enumerated (enum) value 4/21/2005 12:20 PM Logan Greenlee
Thank you!

# re: Convert a string to an enumerated (enum) value 4/28/2005 5:29 AM Markodeablo
Thanks, this has also helped me populate an enum based data class (ultimately being converted to xml).
Cheers,
Mark.

# re: Convert a string to an enumerated (enum) value 7/23/2005 4:46 AM Gary McAllister
You can also use the EnumConvertor class to do the above.

# re: Convert a string to an enumerated (enum) value 9/27/2005 6:09 AM Keval Solanki
thanx for giving me this brain hidden information.

this is very useful 4 me. so my requset to all that just
follow the above code and enjoy..

Keval Solanki
Software Engineer,tatvasoft

# re: Convert a string to an enumerated (enum) value 9/27/2005 6:09 AM Keval Solanki
thanx for giving me this brain hidden information.

this is very useful 4 me. so my requset to all that just
follow the above code and enjoy..

Keval Solanki
Software Engineer,tatvasoft

# re: Convert a string to an enumerated (enum) value 1/11/2006 12:28 AM Offshore Software Outsourcing Development
Ya It's great idea and it's really work.

Thank you once again for put a nice info on converting string I hope you will put more information on that in future.

Jim
TatvaSoft

# re: Convert a string to an enumerated (enum) value 2/9/2006 10:37 AM kmn
THANK YOU!

# re: Convert a string to an enumerated (enum) value 3/7/2006 6:17 AM Pećko
Ma turbo - nema šta

# re: Convert a string to an enumerated (enum) value 4/18/2006 10:00 AM Allan
Awesome... I needed this and googled:

enum C# string

and there you were... Thanks...
I wonder how people learned how to program back in the 80es... (I know.. I borrowed books at the local liberary - Comal80 and Basic)...

But the internet has certainly had a huge impact on the learning curve when it comes to programming... and trouble shooting...


# re: Convert a string to an enumerated (enum) value 4/21/2006 1:43 AM Ismail
That was very usefull. A small thing but of great use

# re: Convert a string to an enumerated (enum) value 5/25/2006 7:18 AM twinsen
tnx a lot mate! didn't know that was possible..

# re: Convert a string to an enumerated (enum) value 5/29/2006 12:34 AM badrepent
Good code.Thanks a lot !!! i've used it combined with a DataReader GetString method.

# re: Convert a string to an enumerated (enum) value 8/25/2006 2:58 AM YE
thanks. just what i needed and top in google !

# re: Convert a string to an enumerated (enum) value 10/12/2006 1:59 AM LJ
Small but perfectly formed - like my wife !

# re: Convert a string to an enumerated (enum) value 11/9/2006 1:16 PM Hamid
oh man, I used to use array for this kind of converting. Thanks a lot!

# re: Convert a string to an enumerated (enum) value 2/12/2007 6:39 AM PStJTT
I'll join the chorus... great tip and a real forehead-slapper (d'oh! Of Course!!!!)

# re: Convert a string to an enumerated (enum) value 4/13/2007 8:13 AM please help
Can you please show how to convert a sting to Enumeration in Java..I can not find any Java examples!! Please help!

# re: Convert a string to an enumerated (enum) value 5/8/2007 10:37 AM Edmun
I search for "convert to enum", and there you are, nice work, nice work.

# re: Convert a string to an enumerated (enum) value 8/9/2007 1:29 PM Srini
Saved my day

Post Feedback

Title:
Name:
Url:
Comments: 
Enter the code you see: