Iterating an Enum using a function

Waterside
Waterside used Ask the Experts™
on
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 ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.

Author

Commented:
I need to pass the Name of the Enum as a string though.
ste5anSenior Developer

Commented:
Have you tested my example? Why should

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

Open in new window


not solve your problem?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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

Author

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
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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

ste5anSenior Developer

Commented:
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.

Author

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

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

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial