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 ?
WatersideAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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.
0
WatersideAuthor Commented:
I need to pass the Name of the Enum as a string though.
0
ste5anSenior DeveloperCommented:
Have you tested my example? Why should

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

Open in new window


not solve your problem?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

käµfm³d 👽Commented:
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

0
WatersideAuthor Commented:
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
0
käµfm³d 👽Commented:
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

0
ste5anSenior DeveloperCommented:
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.
0
WatersideAuthor Commented:
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 :(
0
ste5anSenior DeveloperCommented:
Well, getting the full qualified name right is not that easy.

Use ?GetType(yourEnum).AssemblyQualifiedName in the immediate window.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WatersideAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.