Look up Enum Value by String

I had a need to take a string value and get it’s equivalent Enum type back.  This is how you can do that in VB.Net.

Public Enum PackageTypes
        Subscription = 1
        Sponsorship = 2
End Enum

Dim EnumValue As PackageTypes

EnumValue = CType([Enum].Parse(GetType(PackageTypes), "Subscription", True), PackageTypes)

 

What if it doesn’t exist? You want to check to see if it does first.

You can check to see like this:

If [Enum].IsDefined(GetType(PackageTypes), "Subscription") Then
    'Run previous code snippet here
    EnumValue = CType([Enum].Parse(GetType(PackageTypes), "Subscription", True), PackageTypes)
End IF

 

Want to Pass In a UserControl into your Class? Yes it’s possible.

Traveling to India