How to: Declare Enumerations (Visual Basic)
You create an enumeration with the Enum
statement in the declarations section of a class or module. You cannot declare an enumeration within a method. To specify the appropriate level of access, use Private
, Protected
, Friend
, or Public
.
An Enum
type has a name, an underlying type, and a set of fields, each representing a constant. The name must be a valid Visual Basic .NET qualifier. The underlying type must be one of the integer types—Byte
, Short
, Long
or Integer
. Integer
is the default. Enumerations are always strongly typed and are not interchangeable with integer number types.
Enumerations cannot have floating-point values. If an enumeration is assigned a floating-point value with Option Strict On
, a compiler error results. If Option Strict
is Off
, the value is automatically converted to the Enum
type.
For information on names, and how to use the Imports
statement to make name qualification unnecessary, see Enumerations and Name Qualification.
To declare an enumeration
Write a declaration that includes a code access level, the
Enum
keyword, and a valid name, as in the following examples, each of which declares a differentEnum
.Private Enum SampleEnum SampleMember End Enum Public Enum SampleEnum2 SampleMember End Enum Protected Enum SampleEnum3 SampleMember End Enum Friend Enum SampleEnum4 SampleMember End Enum Protected Friend Enum SampleEnum5 SampleMember End Enum
Define the constants in the enumeration. By default, the first constant in an enumeration is initialized to
0
, and subsequent constants are initialized to a value of one more than the previous constant. For example, the following enumeration,Days
, contains a constant namedSunday
with the value0
, a constant namedMonday
with the value1
, a constant namedTuesday
with the value of2
, and so on.Public Enum Days Sunday Monday Tuesday Wednesday Thursday Friday Saturday End Enum
You can explicitly assign values to constants in an enumeration by using an assignment statement. You can assign any integer value, including negative numbers. For example, you may want constants with values less than zero to represent error conditions. In the following enumeration, the constant
Invalid
is explicitly assigned the value–1
, and the constantSunday
is assigned the value0
. Because it is the first constant in the enumeration,Saturday
is also initialized to the value0
. The value ofMonday
is1
(one more than the value ofSunday
); the value ofTuesday
is2
, and so on.Public Enum WorkDays Saturday Sunday = 0 Monday Tuesday Wednesday Thursday Friday Invalid = -1 End Enum
To declare an enumeration as an explicit type
Specify the type of the enum by using the
As
clause, as shown in the following example.Public Enum MyEnum As Byte Zero One Two End Enum
See also
- Enumerations and Name Qualification
- 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
- When to Use an Enumeration
- Constants Overview
- Constant and Literal Data Types
- Constants and Enumerations