When to Use an Enumeration 

Enumerations offer an easy way to work with sets of related constants. An enumeration, or Enum, is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.

When to Use an Enumeration

Whenever a procedure accepts a limited set of variables, consider using an enumeration. Enumerations make for clearer and more readable code, particularly when meaningful names are used.

The benefits of using enumerations include:

  • Reduces errors caused by transposing or mistyping numbers.

  • Makes it easy to change values in the future.

  • Makes code easier to read, which means it is less likely that errors will creep into it.

  • Ensures forward compatibility. With enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.

Naming Enumerations

Use a naming convention for enumeration members. When Visual Basic encounters an enumeration member name, an exception may be thrown if other referenced type libraries contain the same name. Use a unique prefix that identifies the values from your application or component.

When referring to a member of an enumeration, you must qualify the member name with the enumeration name or else use the Imports statement. For more information, see Enumerations and Name Qualification.

Predefined Enumerations

Visual Basic provides a number of predefined enumerations, such as FirstDayOfWeek and MsgBoxResult, to facilitate your code. For a list of these see Enumerations Declared by Visual Basic.

See Also

Tasks

How to: Declare Enumerations
How to: Refer to an Enumeration Member
How to: Iterate Through An Enumeration in Visual Basic
How to: Determine the String Associated with an Enumeration Value

Reference

Enum Statement (Visual Basic)

Concepts

Enumerations and Name Qualification
Intrinsic Constants and Enumerations