Link to home
Start Free TrialLog in
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 ?
Avatar of ste5an
ste5an
Flag of Germany image

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.
Avatar of Waterside
Waterside

ASKER

I need to pass the Name of the Enum as a string though.
Have you tested my example? Why should

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

Open in new window


not solve your problem?
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

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
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

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.
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
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks