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"
Debug.WriteLine(MyEnumVal.ToString()) ' The output is "gamma"
Taken from 'Enum Tips' by Alberto Venditti
My book "Choose Your First Product" is available now.
It gives you 4 easy steps to find and validate a humble product idea.