如何:确定与枚举值关联的字符串 (Visual Basic)How to: Determine the String Associated with an Enumeration Value (Visual Basic)

利用 GetValuesGetNames 方法,您可以确定与枚举成员关联的字符串和值。The GetValues and GetNames methods allow you to determine the strings and values associated with enumeration members.

确定与枚举关联的字符串To determine the string associated with an enumeration

  • 使用 GetNames 方法可以检索与枚举成员关联的字符串。Use the GetNames method to retrieve the strings associated with the enumeration members. 此示例声明一个枚举,flavorEnum,然后使用 GetNames 方法显示与每个成员关联的字符串。This example declares an enumeration, flavorEnum, then uses the GetNames method to display the strings associated with each member.

    Public Enum flavorEnum
      salty
      sweet
      sour
      bitter
    End Enum
    
    Private Sub TestMethod()
      MsgBox("The strings in the flavorEnum are:")
      Dim i As String
      For Each i In [Enum].GetNames(GetType(flavorEnum))
          MsgBox(i)
      Next
    End Sub
    

另请参阅See also