Export (0) Print
Expand All

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

attribute list

Optional. List of attributes that apply to this enumeration. You must enclose the Attribute List in angle brackets ("<" and ">").

access modifier

Optional. Specifies what code can access this enumeration. Can be one of the following:

You can specify Protected Friend to allow access from code within the enumeration's class, a derived class, or the same assembly.

Shadows

Optional. Specifies that this enumeration redeclares and hides an identically named programming element, or set of overloaded elements, in a base class. You can specify Shadows only on the enumeration itself, not on any of its members.

enumeration name

Required. Name of the enumeration. For information on valid names, see Declared Element Names.

data type

Required if Option Strict is On. Data type of the enumeration and all its members.

member list

Required. List of member constants being declared in this statement. Multiple members appear on individual source code lines.

Each member has the following syntax and parts: [<attribute list>] member name [ = initializer ]

Part

Description

member name

Required. Name of this member.

initializer

Optional. Expression that is evaluated at compile time and assigned to this member.

EndEnum

Terminates the Enum block.

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.

Behavior

  • 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.

Enum filePermissions
    create = 1
    read = 2
    write = 4
    delete = 8
End Enum
Dim file1Perm As filePermissions
file1Perm = filePermissions.create Or filePermissions.read

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.

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

The following example shows values that include both positive and negative numbers.

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

Community Additions

ADD
Show:
© 2014 Microsoft