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"

 

I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.

(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.