How to: Iterate Through An Enumeration in Visual Basic

Enumerations provide a convenient way to work with sets of related constants, and to associate constant values with names. To iterate through an enumeration, you can move it into an array using the GetValues method. You could also iterate through an enumeration using a For...Each statement, using the GetNames or GetValues method to extract the string or numeric value.

To iterate through an enumeration

  • Declare an array and convert the enumeration to it with the GetValues method before passing the array as you would any other variable. The following example displays each member of the enumeration FirstDayOfWeek as it iterates through the enumeration.

    Dim items As Array
    items = System.Enum.GetValues(GetType(FirstDayOfWeek))
    Dim item As String
    For Each item In items
        MsgBox(item)
    Next
    

See also