Dela via


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

How to: Determine the String Associated with an Enumeration Value

How to: Refer to an Enumeration Member

How to: Pass an Array to a Procedure or Property

Concepts

Enumerations Overview

When to Use an Enumeration

Enumerations and Name Qualification

Enumerations Declared by Visual Basic

Overview of Arrays in Visual Basic