Поделиться через


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

Tasks

How to: Declare Enumerations (Visual Basic)

How to: Determine the String Associated with an Enumeration Value (Visual Basic)

How to: Refer to an Enumeration Member (Visual Basic)

How to: Pass an Array to a Procedure or Property (Visual Basic)

Concepts

Enumerations Overview (Visual Basic)

When to Use an Enumeration (Visual Basic)

Enumerations and Name Qualification (Visual Basic)

Other Resources

Arrays in Visual Basic