如何:声明枚举 (Visual Basic)How to: Declare Enumerations (Visual Basic)

在类或模块的声明部分中,使用 Enum 语句创建枚举。You create an enumeration with the Enum statement in the declarations section of a class or module. 不能在方法中声明枚举。You cannot declare an enumeration within a method. 若要指定适当的访问级别,请使用 PrivateProtectedFriendPublicTo specify the appropriate level of access, use Private, Protected, Friend, or Public.

Enum 类型具有名称、基础类型和字段集,每个字段都表示一个常量。An Enum type has a name, an underlying type, and a set of fields, each representing a constant. 该名称必须是有效的 Visual Basic .NET 限定符。The name must be a valid Visual Basic .NET qualifier. 基础类型必须是整数类型之一-ByteShortLongIntegerThe underlying type must be one of the integer types—Byte, Short, Long or Integer. 默认值为 IntegerInteger is the default. 枚举总是强类型化的,并且不能与整数类型互换。Enumerations are always strongly typed and are not interchangeable with integer number types.

枚举的值不能为浮点值。Enumerations cannot have floating-point values. 如果为枚举分配了 Option Strict On的浮点值,则会产生编译器错误。If an enumeration is assigned a floating-point value with Option Strict On, a compiler error results. 如果 Off``Option Strict,则该值会自动转换为 Enum 类型。If Option Strict is Off, the value is automatically converted to the Enum type.

有关名称的信息,以及如何使用 Imports 语句使名称限定不必要,请参阅枚举和名称限定For information on names, and how to use the Imports statement to make name qualification unnecessary, see Enumerations and Name Qualification.

声明枚举To declare an enumeration

  1. 编写包含代码访问级别、Enum 关键字和有效名称的声明,如下面的示例所示,其中每个都声明了不同的 EnumWrite a declaration that includes a code access level, the Enum keyword, and a valid name, as in the following examples, each of which declares a different Enum.

    Private Enum SampleEnum
            SampleMember
        End Enum
        Public Enum SampleEnum2
            SampleMember
        End Enum
        Protected Enum SampleEnum3
            SampleMember
        End Enum
        Friend Enum SampleEnum4
            SampleMember
        End Enum
        Protected Friend Enum SampleEnum5
            SampleMember
        End Enum
    
  2. 定义枚举中的常量。Define the constants in the enumeration. 默认情况下,枚举中的第一个常量被初始化为 0,后面的常量被初始化为比上一个常数大1的值。By default, the first constant in an enumeration is initialized to 0, and subsequent constants are initialized to a value of one more than the previous constant. 例如,下面的枚举 Days包含一个名为 Sunday 的常量,该常量的值为 0、一个名为 Monday、值为 1、一个名为 Tuesday、值为 2的常量等。For example, the following enumeration, Days, contains a constant named Sunday with the value 0, a constant named Monday with the value 1, a constant named Tuesday with the value of 2, and so on.

    Public Enum Days
      Sunday
      Monday
      Tuesday
      Wednesday
      Thursday
      Friday
      Saturday
    End Enum
    
  3. 可以使用赋值语句将值显式分配给枚举中的常量。You can explicitly assign values to constants in an enumeration by using an assignment statement. 可以分配任何整数值,包括负数。You can assign any integer value, including negative numbers. 例如,您可能希望值小于零的常量表示错误条件。For example, you may want constants with values less than zero to represent error conditions. 在以下枚举中,会将值显式赋给常量 Invalid –1,并为常量 Sunday 分配值 0In the following enumeration, the constant Invalid is explicitly assigned the value –1, and the constant Sunday is assigned the value 0. 由于它是枚举中的第一个常数,因此 Saturday 也初始化为 0值。Because it is the first constant in the enumeration, Saturday is also initialized to the value 0. 1 Monday 的值(一个大于 Sunday的值);Tuesday 的值 2等。The value of Monday is 1 (one more than the value of Sunday); the value of Tuesday is 2, and so on.

    Public Enum WorkDays
      Saturday
      Sunday = 0
      Monday
      Tuesday
      Wednesday
      Thursday
      Friday
      Invalid = -1
    End Enum
    

将枚举声明为显式类型To declare an enumeration as an explicit type

  • 使用 As 子句指定枚举的类型,如下面的示例中所示。Specify the type of the enum by using the As clause, as shown in the following example.

    Public Enum MyEnum As Byte
      Zero
      One
      Two
    End Enum
    

另请参阅See also