tags:

views:

3635

answers:

2

Hi guys, 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?

+6  A: 

See Enum.TryParse.

Anton Gogolev
There is only Parse() method. Not sure where is the Enum.TryParse() method?
David.Chu.ca
@David: Enum.TryParse() is available in .NET 4
Alex Angas
+10  A: 
Dim val = DirectCast([Enum].Parse(GetType(Gender), "Male"), Gender)
Kamarey