How can I parse a string in VB.NET to enum value?
Example I have this enum:
Public Enum Gender
NotDefined
Male
Female
End Enum
how can I convert a string "Male" to the Gender enum's Male value?
Dim val = DirectCast([Enum].Parse(GetType(Gender), "Male"), Gender)
Male. i saved the enum value in database and am trying to get it back. in this case i might not know thw actual value that i saved since am converting toStringParse(enumType As System.Type, value As String) As ObjectDirectCast([Enum].Parse(GetType(Gender), "Male"), Integer)See Enum.TryParse.
how can I convert a string "Male" to the Gender enum's Male value?
The accepted solution returns an Enum object. To return the value you want this solution:
dim MyGender as string = "Male"
dim Value as integer
Value = DirectCast([Enum].Parse(GetType(Gender), MyGender), Integer)
Can also do it this way:
value = cInt([enum].Parse(GetType(Gender), MyGender))