Syntax accessModifier Enum [Shadows] name [As type] memberName [= constantExpression] memberName [= constantExpression] ... End Enum
accessModifier (optional) Specifies the scope and accessibility of the enumeration. One of the following access levels: Access level | Description |
---|
Public | The enumeration is publicly accessible anywhere, both inside and outside of the project. | Private | The enumeration is accessible only within the defining type. This level cannot be used for namespace-level enumerations. | Protected | The enumeration is accessible only to the code in the defining type or to one of its derived types. This level cannot be used for namespace-level enumerations. | Friend | The enumeration is accessible only within the project that contains the enumeration definition. | Protected Friend | Combines the access features of Protected and Friend. This level cannot be used for namespace-level enumerations. |
If omitted, the Public access level is used when declared within types, and Friend is used when declared within namespaces.
Shadows (optional) Indicates that the enumeration shadows an identically named element in a base class.
name (required) The name of the enumerated data type.
type (optional; integral type) The data type of the enumeration. All enumerated members must be integer-based; possible values are Byte, Integer, Long, SByte, Short, UInteger, ULong, and UShort. If omitted, the default type is Integer.
memberName (required) The name of a member of the enumerated data type.
constantExpression (optional; integral type) The value to be assigned to memberName. Description The Enum statement defines an enumerated data type. All of the members and related values of the data type are defined by the memberName entries. Usage at a Glance The Enum statement can appear at the namespace level and within types such as classes, but not within members such as procedures. constantExpression can be either a negative or a positive number. It can also be another member of an enumerated data type or an expression that includes integers, constants, and enumerated data types. The rules that apply to assigning values to constants also apply to enumeration members. See the Const Statement entry in this chapter for more information. If you assign a floating point value to constantExpression, it is automatically rounded and converted to an integer only if Option Strict is Off; otherwise, it generates a compiler error. If constantExpression is omitted, the value assigned to memberName is 0 if it is the first expression in the enumeration. Otherwise, its value is 1 greater than the value of the preceding memberName. The values assigned to memberName entries cannot be modified at runtime. Once you define an enumerated type, you can use name as you would any other data type. Enumerated data type members appear as value types. The compiler does not enforce range restrictions on variables declared using enumerated data types. If an enumeration includes entries for integer values 1 through 5, your code can still assign a value of 10 to the variable, even though 10 does not represent one of the enumerated values. Individual values of an enumerated type can be used in your program just like normal constants, except that they must be prefaced with the name of the enumeration. If you want to retrieve or display the name of an enumerated member rather than its value, you can use the member's ToString method. For example: Public Module GeneralCode Public Enum WorkDayTypes Weekday = 0 Weekend = 1 Holiday = 2 Floating = 3 Personal = 4 Vacation = 5 End Enum Public Sub TestEnum( ) Dim dayType As WorkDayTypes = WorkDayTypes.Vacation MsgBox(dayType.ToString( )) ' Displays "Vacation" End Sub End Module Example Public Enum AnnualQuarter FirstQuarter = 1 SecondQuarter = 2 ThirdQuarter = 3 FourthQuarter = 4 End Enum Version Differences In VB 6, members of an enumeration can be accessed without having to qualify them with the name of the enumeration to which they belong. In .NET, this behavior is not permitted; the members of an enumeration can only be accessed by referring to the name of their enumeration. In VB 6, all enumerated members are of type Long. .NET allows you to define the integral data type of the enumeration's members. In VB 6, members of a public enumeration can be hidden from the Object Browser by adding a leading underscore to the member name. For example, in the enumeration: Public Enum Primes [_x0] = 0 x1 = 1 x2 = 3 End Enum the constant _x0 is hidden in IntelliSense and the Object Browser unless the Object Browser's "Show Hidden Members" option is selected. In .NET, a leading underscore does not hide a member. See Also Const Statement |