How to match a string value to its equivalent Enum value.

Updated! How to retrieve an Enum member given its name

To retrieve an Enum member given its name, use the Parse method of the Enum class, and cast the result to your Enum type:


MyEnumVal = CType(System.Enum.Parse(GetType(MyEnum), "gamma"), MyEnum)
Debug.WriteLine(MyEnumVal.ToString()) ' The output is "gamma"

Previous code:

To retrieve an Enum member given its name, use the ConvertFrom() method of the TypeConverter class and cast the result to your Enum type:

MyEnumVal = CType(TypeDescriptor.GetConverter(MyEnumVal).ConvertFrom("gamma"), MyEnum)
Debug.WriteLine(MyEnumVal.ToString()) ' The output is "gamma"

 

My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.