Enum 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"
Try
Dim status1 As Status = CType([Enum].Parse(GetType(Status), number), Status)
If Not [Enum].IsDefined(GetType(Status), status1) Then status1 = Status.Unknown
Console.WriteLine("Converted '{0}' to {1}", number, status1)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to an Status value.",
number)
End Try
End Sub
End Module
Related examples in the same category