Skip to Content
Programming Visual Basic .NET
book

Programming Visual Basic .NET

by Dave Grundgeiger
December 2001
Beginner
464 pages
13h 51m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET

Enumerations

An enumeration is a type whose values are explicitly named by the creator of the type. The .NET Framework and Visual Basic .NET define many enumerations for their and your use. In addition, Visual Basic .NET provides syntax for defining new enumerations. Here is an example:

Public Enum Rainbow
   Red
   Orange
   Yellow
   Green
   Blue
   Indigo
   Violet
End Enum

This declaration establishes a new type, called Rainbow. The identifiers listed within the body of the declaration become constant values that may be assigned to variables of the Rainbow type. Here is a declaration of a variable of type Rainbow and an initial assignment to it:

Dim myRainbow As Rainbow = Rainbow.Blue

Note that the value name is qualified by the type name.

Enumerations are value types that implicitly inherit from the .NET Framework’s System.Enum type (which in turn inherits from System.ValueType). That means that every enumeration has access to the members defined by System.Enum. One such member is the ToString method, which returns a string containing the name of the value. This is handy for printing:

Dim myRainbow As Rainbow = Rainbow.Blue
Console.WriteLine("The value of myRainbow is: " & myRainbow.ToString(  ))

This code results in the following output:

The value of myRainbow is: Blue

The values of an enumeration are considered as ordered. Thus, comparisons are permitted between variables of the enumeration type:

Dim myRainbow As Rainbow Dim yourRainbow As Rainbow ' ... If myRainbow < yourRainbow Then ' ... End If ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET, Second Edition

Programming Visual Basic .NET, Second Edition

Jesse Liberty

Publisher Resources

ISBN: 0596000936Supplemental ContentCatalog PageErrata