user774411
user774411

Reputation: 1809

How to list Enum's members

How to list Enum's members in code? I have the following Enum:

Public Enum TestEnum As int32
    First = 0
    Second = 2
    Third = 4
    Fourth = 6
End Enum

And I try to list all members of TestEnum via the following code but it failed:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Enum1 As TestEnum

        Dim Members() As String

        Members = System.Enum.GetNames(CType(Enum1, System.Enum))


    End Sub
End Class

So, my question is: How to list members of an Enum?

Update: The solution is:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Members() As String
        Members = System.Enum.GetNames(GetType(TestEnum))

        MessageBox.Show(Join(Members, Chr(13) & Chr(10)))

    End Sub
End Class

Upvotes: 28

Views: 40432

Answers (4)

Paul Karam
Paul Karam

Reputation: 4210

I have used George Filippakos answer since I wanted to know how to iterate through Enum values.

I also found out that you can do it using Type.GetEnumValues which has been available since .NET Framework 4.0.

Here's the two ways you can use to iterate through Enum Values:

Module Module1
    Sub Main()
        For Each tstEnum As TestEnum In System.Enum.GetValues(GetType(TestEnum))
            Console.WriteLine($"Name: {tstEnum.ToString}, Value: {CType(tstEnum, Integer)}")
        Next

        Console.WriteLine(Environment.NewLine)

        For Each tstEnum As TestEnum In GetType(TestEnum).GetEnumValues
            Console.WriteLine($"Name: {tstEnum.ToString}, Value: {CType(tstEnum, Integer)}")
        Next

        Console.ReadKey()
    End Sub

    Public Enum TestEnum
        First = 1
        Second = 2
        Third = 3
    End Enum
End Module

Output:

Name: First, Value: 1
Name: Second, Value: 2
Name: Third, Value: 3

Name: First, Value: 1
Name: Second, Value: 2
Name: Third, Value: 3

Upvotes: 10

George Filippakos
George Filippakos

Reputation: 16569

You can simply iterate through all values like this:

Public Enum TestEnum As int32
    First = 0
    Second = 2
    Third = 4
    Fourth = 6
End Enum

For Each tstEnum As TestEnum In System.Enum.GetValues(GetType(TestEnum))

    Response.Write(
        String.Format("Name: {0}  Value: {1}", 
            tstEnum.ToString, 
            CInt(tstEnum).ToString
        )
    )

Next

Upvotes: 29

Brandon Moretz
Brandon Moretz

Reputation: 7621

Have you looked at Enum.GetValues ?

Edit: To clarify, yes you need to pass a Type not an instance of the enum to either method.

Enum.GetNames(GetType(TestEnum))

Upvotes: 9

Sven
Sven

Reputation: 22683

You need to pass a type, not a value, to the method.

Members = System.Enum.GetNames(GetType(TestEnum))

If you have an instance of your enum you can also use

Members = System.Enum.GetNames(Enum1.GetType())

Though I would recommend the first approach if you know the type you want.

Upvotes: 19

Related Questions