如何:在 Visual Basic 中循环访问枚举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. 若要循环访问枚举,可以使用 GetValues 方法将其移到数组中。To iterate through an enumeration, you can move it into an array using the GetValues method. 还可以使用 For...Each 语句来循环访问枚举,方法是使用 GetNamesGetValues 方法提取字符串或数字值。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

  • 声明数组,并使用 GetValues 方法将枚举转换为它,然后再传递数组,就像传递任何其他变量一样。Declare an array and convert the enumeration to it with the GetValues method before passing the array as you would any other variable. 下面的示例在循环访问枚举时显示枚举 FirstDayOfWeek 的每个成员。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