Visual Basic Language Reference  

Enum Statement

Used at module, class, or structure level to declare an enumeration and define the values of its members.

<attrlist> ] [{ Public | Protected | Friend | Protected Friend | 
Private }] [ Shadows ] Enum name [ As type ]
   [<attrlist1>] membname1 [ = initexpr1 ]
   [<attrlist2>] membname2 [ = initexpr2 ]
...
   [<attrlistn>] membnamen [ = initexprn ]
End Enum

Parts

attrlist
Optional. List of attributes that apply to the enumeration or to this member. Multiple attributes are separated by commas.
Public
Optional. Enumerations declared with the Public keyword have public access. There are no restrictions on the accessibility of public enumerations.
Protected
Optional. Enumerations declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Enumerations declared with the Friend keyword have friend access. They are accessible from within the program that contains their declaration and from anywhere else in the same assembly.
Protected Friend
Optional. Enumerations declared with the Protected Friend keywords have the union of protected and friend access. They can be used by code in the same assembly, as well as by code in derived classes. Protected friend access can be specified only on members of classes.
Private
Optional. Enumerations declared with the Private keyword have private access. They are accessible only from within their declaration context, including from members of any nested types such as procedures.
Shadows
Optional. Indicates that this enumeration shadows an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable in the derived class that shadows it.
name
Required. Name of the enumeration. Must be a valid Visual Basic identifier. When you subsequently declare variables or arguments to be of this Enum type, you specify name for their data type.
type
Optional, even if Option Strict is On. Data type of the enumeration and all of its members. Can be Byte, Integer, Long, or Short. If you do not specify type, it defaults to Integer.
membname
Required. Name of this member of the enumeration. Must be a valid Visual Basic identifier.

The accessibility of every member is Public, and you cannot declare it otherwise. However, if the enumeration itself is declared with Protected, Friend, Protected Friend, or Private access, this limits the accessibility of the members.

initexpr
Optional. Expression that is evaluated and assigned to this member when the enumeration is created. Can consist of a literal; a constant that is already defined; another enumeration member; or any combination of literals, constants, and enumeration members. You can use arithmetic and logical operators to combine such elements.

You cannot use variables or functions in initexpr. 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.

If you do not specify initexpr, the value assigned is either zero (if it is the first membname), or greater by one than the value of the immediately preceding membname.

End Enum
Terminates an Enum block.

Each attribute in the attrlist part has the following syntax and parts:

attrname [(attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Remarks

The Enum statement can appear only at module, namespace, or file level. This means you can declare enumerations in a source file or inside a module, class, or structure, but not inside a procedure. Once the Enum type is defined, it can be used to declare the type of variables, procedure arguments, and Function returns.

Enumerations can be accessed from anywhere within the module, class, or structure in which they are declared. An enumeration and all its members are Public by default. To specify the accessibility in more detail, include Public, Protected, Friend, Protected Friend, or Private in the Enum statement. The accessibility you specify applies to all the members as well as to the enumeration itself.

An enumeration is a related set of constants. The enumeration members between the Enum and End Enum statements are initialized to constant values. The defined values cannot be modified at run time. Values can include both positive and negative numbers, as the following example shows:

Enum SecurityLevel
   IllegalEntry = -1
   MinimumSecurity = 0
   MaximumSecurity = 1
End Enum

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. If Option Strict is On, you can assign only enumeration members to the enumeration variable. In this case, you can use the CType keyword to explicitly convert a numeric data type to an Enum type.

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 as SecurityLevel.IllegalEntry, but not as IllegalEntry.

Example

This example uses the Enum statement to define a set of named constants. In this case, the constants are colors you might choose to design data entry forms for a database.

Public Enum InterfaceColors
   MistyRose   = &HE1E4FF&
   SlateGray   = &H908070&
   DodgerBlue  = &HFF901E&
   DeepSkyBlue = &HFFBF00&
   SpringGreen = &H7FFF00&
   ForestGreen = &H228B22&
   Goldenrod   = &H20A5DA&
   Firebrick   = &H2222B2&
End Enum

See Also

Const Statement | Dim Statement | Implicit and Explicit Conversions | Type Conversion Functions | Asc, AscW Functions