0% found this document useful (0 votes)
4 views

complete-reference-vb_net_11

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

complete-reference-vb_net_11

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Enumerations

The user−defined Enum inherits from System.Enum, which in turn inherits from System.ValueType. It is
processed in stack memory.

In addition to being an efficiently managed type, the Enum offers exceptional reference semantics. It is an
elegantly implemented type, infused with a very useful collection of methods that render the Enum one of the
most pleasurable constructs to work with in the .NET Framework. Enumerated types, like interfaces, are part
of the fabric that makes up the web and woof of the framework.

Here's an example of using the enumeration: Let's say we need to set up a grid on a monitor for
renderingplotting the flight path of a space ship across a sector in space, which the grid must refer to and
represent. The grid would function as a "window" to the sector in space.

We need some frame of reference for the grid and a collection of methods that manage it in relation to the
inbound data it's rendering. For starters, the grid would be referenced on its left bound at pixel 0 and on the
right bound at pixel 480 (for argument's sake). These values might represent the length of a monitor's viewing
areas, but they could also represent the grid within a user interface (probably surrounded by instrumentation).

As the space ship travels, the grid represents its path, but remains unchanged. The monitor would have to pan
in order to track the object as it approaches the edge of the grid's field of view. A method for this would
regularly test the ship's location; if it were near or on one of the edges, the grid would move to keep it in view.
The grid could remain fixed until the object approached the bounds, or it could fix the space ship onto
coordinates and continuously pan as the ship moves, and stop as the ship rests.

Using magic numbers to compute the trajectory of the object would make the code hard to read. For example,
with a magic number the code would read as follows:

If (ObjectPosition >= 75) Then


'. . . do something
End If

Better to use a named constant as follows:

If (ObjectPosition >= GridVectorsEnum.MaxRight) Then


'. . . do something
End If

If the numeric value of MaxRight changes from 75 to 65, we need to change the constant in only one place in
our software. Using the magic number, we would be forced to change the value from 75 to 65 wherever it was
referenced. Manually replacing the unnamed constants would be laborious and impractical. The resulting code
would be both hard to read and prone to bugs, especially in a large program.

If we needed to maintain a large set of coordinates for our grid, we could list a collection of them in an
enumeration and assign them the constants of their respective grid values. The following example uses the
Enum to represent such a collection of grid constants:

Public Enum GridVectorsEnum


MaxRight = 480
MaxLeft = 0
MaxHeight = 300
MinHeight = 40
PixelDistance = 12 'the number of pixels to move left
'for right for each Kilometer traveled
End Enum

249

You might also like