Enum parsing in C#

January 21, 2006


Enum parsing in C#

Frequently, you will have a situation where you store the string representation of an enumerated value in a file or database, and when it is retrieved you have to re-assign it to a variable that is of the enumerated type. You can do this as follows:

public enum MyEnumeratedType
     Foo = 0, Bar = 1

MyEnumeratedType enumVariable = (MyEnumeratedType) Enum.Parse(typeof(MyEnumeratedType), “Bar”, true);

With all due respect to Anders, this is NUTS!!! I wish there was a simpler way…

Co-founder/CTO WhenHub; co-founder DNN Software; founder Edaptable; educator; Open Source proponent; Microsoft MVP; tech geek; creative thinker; husband; dad. Personal blog: http://www.kalyani.com. Twitter: @techbubble
Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.