Enumerations The Visual Basic .NET Language

91 Public Shared Sub PrintParamArray ByVal objArray As Object Dim obj As Object For Each obj In objArray ... Next End Sub Clearly, objArray is an array of reference types, and obj is a reference type. Yet it would be nice to pass value types and reference types to the method, like this: Printhello, world, SomeObject, 4, True In fact, this is possible. When a value type is assigned to a variable of type Object or passed in a parameter of type Object, it goes through a process known as boxing. To box a value type means to allocate memory to hold a copy of the value, then copy the value into that memory, and finally manipulate or store a reference to the value. Unboxing is the opposite process: taking a reference to a value type and copying the referenced value into an actual value type. Boxing and unboxing are done on your behalf by the .NET runtime—there is nothing you have to do to facilitate it. You should be aware of it, however, because the box and unbox operations arent free.

2.17 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 Frameworks 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.WriteLineThe value of myRainbow is: myRainbow.ToString This code results in the following output: 92 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 Variables of an enumeration type can be used as indexes in For...Next statements. For example: For myRainbow = Rainbow.Red To Rainbow.Violet ... Next Internally, Visual Basic .NET and the .NET Framework use values of type Integer to represent the values of the enumeration. The compiler starts with 0 and assigns increasing Integer values to each name in the enumeration. It is sometimes useful to override the default Integer values that are assigned to each name. This is done by adding an initializer to each enumeration constant. For example: Public Enum MyLegacyErrorCodes NoError = 0 FileNotFound = -1000 OutOfMemory = -1001 InvalidEntry = -2000 End Enum It is also possible to specify the type of the underlying value. For example: Public Enum Rainbow As Byte Red Orange Yellow Green Blue Indigo Violet End Enum This could be an important space-saving measure if many values of the enumeration will be stored somewhere. The only types that can be specified for an enumeration are Byte, Short, Integer, and Long. Sometimes enumerations are used as flags, with the idea that multiple flags can be combined in a single value. Such an enumeration can be defined by using the Flags attribute. Attributes are discussed later in this chapter. Here is an example: Flags Public Enum Rainbow Red = 1 Orange = 2 Yellow = 4 Green = 8 Blue = 16 Indigo = 32 93 Violet = 64 End Enum Note two important things in this definition: • The first line of the definition starts with Flags . This indicates that values of this type can be composed of multiple items from the enumeration. • The items in the enumeration have values that are powers of two. This ensures that each combination of items has a unique sum. For example, the combination of Yellow, Blue, and Violet has a sum of 84, which cant be attained with any other combination of items. Individual values are combined using the Or operator. The ToString method is smart enough to sort out the value names when creating a string representation of the value. For example, given the previous assignment, consider the following call to the ToString method: Console.WriteLinemyRainbow.ToString This statement produces the following output: Green, Blue

2.18 Exceptions