Avatar of Waterside
Waterside
 asked on

Iterating an Enum using a function

I have this code to extract the members from an Enum.

Dim items As Array
        items = System.Enum.GetValues(GetType(someEnum))
        Dim item As String
        For Each item In items
            MessageBox.Show(item)
        Next

Open in new window


How can I turn this into a function that takes the Enum Name as a string ?
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Waterside

8/22/2022 - Mon
ste5an

It's quite easy:

Namespace Test
    Enum EnumTest
        etOne
        etTwo
        etThree
    End Enum
End Namespace

Module Module1
    Sub Main()

        Dim item As String

        For Each item In System.Enum.GetValues(GetType(Test.EnumTest))
            Console.WriteLine(item)
        Next

        For Each item In System.Enum.GetNames(GetType(Test.EnumTest))
            Console.WriteLine(item)
        Next

        Dim enumValue As Test.EnumTest

        enumValue = System.Enum.Parse(GetType(Test.EnumTest), "etTwo")
        Console.WriteLine(System.Enum.GetName(GetType(Test.EnumTest), enumValue))

        enumValue = System.Enum.Parse(Type.GetType("Console_VB.Test.EnumTest", True), "etTwo")
        Console.WriteLine(System.Enum.GetName(Type.GetType("Console_VB.Test.EnumTest"), enumValue))

        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub
End Module

Open in new window


You need only the fully qualified name of the type.
Waterside

ASKER
I need to pass the Name of the Enum as a string though.
ste5an

Have you tested my example? Why should

Type.GetType("Console_VB.Test.EnumTest", True)

Open in new window


not solve your problem?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
kaufmed

Module Module1

    Sub Main()
        Dim c As ConsoleColor = GetEnumByName("test")
    End Sub

    Function GetEnumByName(ByVal name As String) As ConsoleColor
        For Each item In [Enum].GetNames(GetType(ConsoleColor))
            If String.Equals(name, item, StringComparison.OrdinalIgnoreCase) Then
                Return DirectCast([Enum].Parse(GetType(ConsoleColor), name), ConsoleColor)
            End If
        Next

        Return DirectCast(-1, ConsoleColor) ' You may want to change this to some other default value
    End Function

End Module

Open in new window


I'm using ConsoleColor here, but you can change that to match your enum of concern. Also, I've specified a default return of -1 if the enum value isn't found, but you can change that to a more fitting value. If you'd rather have Nothing returned, then you'll need to change the function definition to return a Nullable enum instead:

Module Module1

    Sub Main()
        Dim c As ConsoleColor = GetEnumByName("test")
    End Sub

    Function GetEnumByName(ByVal name As String) As Nullable(Of ConsoleColor)
        For Each item In [Enum].GetNames(GetType(ConsoleColor))
            If String.Equals(name, item, StringComparison.OrdinalIgnoreCase) Then
                Return DirectCast([Enum].Parse(GetType(ConsoleColor), name), ConsoleColor)
            End If
        Next

        Return Nothing
    End Function

End Module

Open in new window

Waterside

ASKER
Ah maybe a re-phrase is in order ?

I want to return the Members of a given Enum.

So with Ste5an's EnumTest...

Function getEnumMembersByEnumName(enumName As String)

Would give..

        etOne
        etTwo
        etThree
kaufmed

Using ste5an's example, are you saying that your function would receive "EnumTest" as input?

e.g.

Function SomeFunction(ByVal enumName As String) As String()
...

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ste5an

It's alread in my sample:

System.Enum.GetNames(Type.GetType(enumName , True))

Open in new window


Just make your function return an array and add proper exception handling when necessary.
Waterside

ASKER
The Enum is defined a Public in a User Control.

I can use Ste5an's

Type.GetType("enumTheme", True)

Open in new window


from within the control, but when I try to use it from the page that the control is in I get an ArguementNullExeption.  I've tried using assembly.namespace.enum but no luck :(
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Waterside

ASKER
Thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy