C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
GetType: The GetType Function is used with the argument VehicleType. This is the Type argument to the Enum.Parse Function.
Brackets: The Enum type uses a special syntax where it is surrounded by square brackets. This indicates a Type, not the Enum keyword.
VB.NET program that uses Enum.Parse
Module Module1
''' <summary>
''' Types of vehicles.
''' </summary>
Enum VehicleType
None
Truck
Sedan
Coupe
End Enum
Sub Main()
' Convert String to Enum.
Dim value As String = "Truck"
Dim type As VehicleType = [Enum].Parse(GetType(VehicleType), value)
' Test Enum.
If type = VehicleType.Truck Then
Console.WriteLine("Equals truck")
End If
End Sub
End Module
Output
Equals truck
VB.NET program that handles Parse, Exception
Module Module1
Enum VehicleType
None
Truck
Sedan
Coupe
End Enum
Sub Main()
' An invalid String.
Dim value As String = "Spaceship"
Try
' Parse the invalid String.
Dim type As VehicleType = [Enum].Parse(GetType(VehicleType), value)
Catch ex As Exception
' Write the Exception.
Console.WriteLine(ex)
End Try
End Sub
End Module
Output
System.ArgumentException: Requested value 'Spaceship' was not found.
at System.Enum.EnumResult.SetFailure...
VB.NET program that uses TryParse
Module Module1
Enum VehicleType
None
Truck
Sedan
End Enum
Sub Main()
' An invalid String.
Dim value As String = "Starship"
' Parse the invalid String.
Dim result As VehicleType
[Enum].TryParse(value, result)
' It is the default.
Console.WriteLine(result.ToString())
End Sub
End Module
Output
None