|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Enum Statement (Visual Basic)
Declares an enumeration and defines the values of its members.
[ <attribute list> ] [ access modifier ] [ Shadows ] Enum enumeration name [ As data type ] member list End Enum
If you have a set of unchanging values that are logically related to each other, you can define them together in an enumeration. This provides meaningful names for the enumeration and its members, which are easier to remember than their values. You can then use the enumeration members in many places in your code. This also improves the readability of your code because all the related values use the same enumeration name.
You can use Enum only at namespace or module level. This means the declaration context for an enumeration must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure. For more information, see Declaration Contexts and Default Access Levels.
The Enum statement can declare the data type of an enumeration. Each member takes the enumeration's data type. You can specify Byte, Integer, Long, SByte, Short, UInteger, ULong, or UShort.
If you do not specify initializer for a member, Visual Basic initializes it either to zero (if it is the first member in member list), or to a value greater by one than that of the immediately preceding member.
Class, structure, module, and interface member enumerations default to public access. You can adjust their access levels with the access modifiers. Namespace member enumerations default to friend access. You can adjust their access levels to public, but not to private or protected. For more information, see Access Levels in Visual Basic.Rules
Declaration Context. An enumeration declared at module level, outside any procedure, is a member enumeration; it is a member of the class, structure, module, or interface that declares it.
An enumeration declared at namespace level, outside any class, structure, module, or interface, is a member only of the namespace in which it appears.
Attributes. You can apply attributes to an enumeration as a whole, but not to its members individually. An attribute contributes information to the assembly's metadata.
Modifiers. By default, all enumerations are types and their fields are constants. Therefore the Shared, Static, and ReadOnly keywords cannot be used when declaring an enumeration or its members.
Data Type Rules
Default Type. If you do not specify data type for the enumeration, each member takes the data type of its initializer. If you specify both data type and initializer, the data type of initializer must be convertible to data type. If neither data type nor initializer is present, the data type defaults to Integer.
Initialization. The Enum statement can initialize the contents of selected members in member list. You use initializer to supply an expression to be assigned to the member.
The expression supplied in each initializer can be any combination of literals, other constants that are already defined, and enumeration members that are already defined, including a previous member of this enumeration. You can use arithmetic and logical operators to combine such elements.
You cannot use variables or functions in initializer. However, you can use conversion keywords such as CByte and CShort. You can also use AscW if you call it with a constant String or Char argument, since that can be evaluated at compile time.
Access Level. All enumeration members have public access, and you cannot use any access modifiers on them. However, if the enumeration itself has a more restricted access level, the specified enumeration access level takes precedence..
Scope. Member enumerations are accessible from anywhere within their class, structure, module, or interface. Namespace member enumerations are accessible from any code within that namespace.
Qualification. Code outside a class, structure, or module must qualify a member enumeration's name with the name of that class, structure, or module.
Invalid Values. If the value of a member exceeds the allowable range for the underlying data type, or if you initialize any member to the maximum value allowed by the underlying data type, the compiler reports an error.
Enumeration variables are variables declared to be of an Enum type. Declaring a variable in this way helps you to control the values you assign to it. However, you can still assign a value that is not an enumeration member, provided its data type can be converted to the enumeration's data type. This is useful when the enumeration is a flag field and you assign a combination of flags to the enumeration variable. The following example shows an assignment of multiple flags to an enumeration variable.
You must qualify every reference to an enumeration member, either with the name of an enumeration variable or with the enumeration name itself. For example, in the preceding example, you can refer to the first member asfilePermissions.create, but not as create.
The following example uses the Enum statement to define a related set of named constant values. In this case, the values are colors you might choose to design data entry forms for a database.
The following example shows values that include both positive and negative numbers.