Enumerations, one of the core .NET types, allow you to group named, related integer values as a set. Once bound together, the enumeration can be used like any other data type; you can create variables that are specific instances of an enumeration.
Enumerations are a multi-line construct; the first line defines the name and underlying data type of the enumeration. Each enumeration member appears on a separate line, ending with a final closing End Enum line.
01 Enum CarType As Integer 02 Sedan = 1 03 StationWagon = 2 04 Truck = 3 05 SUV = 4 06 Other = 5 07 End Enum
The declaration line (line 01) includes the Enum keyword, the name of the enumeration (CarType) and the underlying data type (Integer). The As data type clause is optional; if you leave it off, the enumeration defaults to Integer. If you do supply a data type, it must be one of the following: Byte, Integer, Long, SByte, Short, UInteger, ULong, or UShort.
Each member of the enumeration (lines 02 to 06) must include at least a member name (such as Sedan). You can optionally assign a numeric value to some or all of the members, as I have done in the sample. If a member lacks an assignment, it is set to one more than the previous member. If none of the members have an assigned value, the first is assigned 0, the next to 1, and so on.
Once defined, enumeration members act a lot like integer constants; you can use them anywhere you would normally use a literal or constant. When referencing the members of an enumeration in your code, include both the enumeration name and the member name.
The Enum statement cannot be used within a method or procedure. Instead, you define an enumeration as a member of a type (class, structure, or module), or as its own stand-alone type, just like a class. The .NET Framework includes many useful predefined enumerations intended for use with Framework features. For instance, the System.DayOfWeek enumeration includes members for each day of the week.