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
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.
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.
Each attribute in the attrlist part has the following syntax and parts:
attrname [({ attrargs | attrinit })]
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
.
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
Const Statement | Dim Statement |