Home
VB.NET
Enum.Parse: Convert String to Enum
Updated Aug 1, 2024
Dot Net Perls
Enum.Parse. Sometimes a String must be converted into an Enum. This requires a special Function call. In VB.NET we use the Enum.Parse, or TryParse, Functions.
Method notes. We handle exceptions and errors for each method. Often TryParse is a better choice if our data is not already validated.
Enum
First example. This example introduces an Enum of name PetType. In the Main Sub, we have a String literal with value "Dog". We call [Enum].Parse to get its same-named Enum.
Info The GetType Function is used with the argument VehicleType. This is the Type argument to the Enum.Parse Function.
Important The Enum type uses a special syntax where it is surrounded by square brackets. This indicates a Type, not the Enum keyword.
Module Module1 ''' <summary> ''' Types of pets. ''' </summary> Enum PetType None Cat Dog End Enum Sub Main() ' Convert String to Enum. Dim value As String = "Dog" Dim type As PetType = [Enum].Parse(GetType(PetType), value) ' Test Enum. If type = PetType.Dog Then Console.WriteLine("Equals dog") End If End Sub End Module
Equals dog
Example 2. Next, we learn that Enum.Parse will throw an Exception on an invalid String. One way to handle this problem is to use a Try-Catch block to capture possible errors.
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
System.ArgumentException: Requested value 'Spaceship' was not found. at System.Enum.EnumResult.SetFailure...
TryParse. Next, the TryParse Function is available. It handles errors in a safer, faster way. It does not throw an Exception on an invalid String that cannot be converted.
Return Enum.TryParse returns False if the string cannot be converted. The Enum is set to the default value.
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
None
A summary. We require no custom conversion methods to parse Enums in VB.NET programs. Instead, the Enum.Parse and TryParse Functions fill this need.
Final notes. If an invalid String may be encountered, the TryParse Function is often superior. Avoiding exceptions is faster in most programs.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 1, 2024 (edit link).
Home
Changes
© 2007-2025 Sam Allen