Use TryParse to parse string value to Enum
Public Enum Status As Integer
Unknown = -3
Late = -1
OnTime = 0
Early = 1
End Enum
Public Module Example
Public Sub Main()
Dim number As String = "-1"
Dim name As String = "Early"
Dim invalid As String = "32"
Dim status2 As Status
If [Enum].TryParse(Of Status)(name, status2) Then
If Not [Enum].IsDefined(GetType(Status), status2) Then status2 = Status.Unknown
Console.WriteLine("Converted '{0}' to {1}", name, status2)
Else
Console.WriteLine("Unable to convert '{0}' to an Status value.",
number)
End If
End Sub
End Module
Related examples in the same category