0% found this document useful (0 votes)
5 views1 page

Complete-Reference-Vb Net 10

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)
5 views1 page

Complete-Reference-Vb Net 10

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

provide a parameterless constructor.

Note It is also worth noting that structures cannot take destructors, because they are not collected by the
garbage collector. When they are no longer needed the CLR knows to get rid of them.

Enumerations

In Chapter 4 we talked a little about magic numbers, the constant ordinals that represent array bounds,
character positions, coordinates, subscripts, indexes, flags, and other literal number values in your algorithms.
Whenever you need to refer to these values in your software, you must name the magic numbers before you
use them; otherwise, your code becomes extremely hard to read and maintainespecially when these numbers
appear suddenly and without explanations.

As a rule, any number other than 0 or 1 will likely be magical and should be given an identifier, its name. All
modern software−development languages support named constants and global variables, so there is no excuse
for leaving magic numbers in your code.

When you need to work with a list of constants that represent magic numbers, organize them in a group for
uniform reference and ease of management. Standard classes and structures would be the first "containers"
that come to mind for encapsulating named constants. Classes, however, are too cumbersome and would
always place your collection of constants in heap space. Accessing the members of the class requires
object−reference semantics, which is clumsy for something so basic. Access and visibility are also
considerations for constants encapsulated at the standard−class level.

A structure would be a better container because it's efficient, although it's not ideal. Fortunately, the .NET
Framework is equipped with an enumeration or enumerated typethe Enumthat provides that facility for you.

Note Do not confuse the term enumeration with the term enumerator, which is an object that iterates
over a collection, or counts a collection of values (see Chapter 12).

The Enum is a value type, like a structure; however, it does not have a structure's facilities for methods and
properties, which represent baggage in this context. It's only a place for naming your constants, a place for
hiding magic numbers. Enum types are thus ideal to represent collections of symbols that correspond to
ordinal values.

Within these enumerations, you can manage the symbols in familiar terms as your named constants. And you
don't need to explicitly initialize them with values. In addition to having a formal type for symbolic names,
enumerations provide a strong type utility. In other words, you cannot pass the value
TemparatureEnum.Hot to a method that requires an argument of type BouquetEnum.Roses. It is also more
helpful in debugging and documentation to be shown meaningful symbols rather than meaningless numbers.
You can easily see what enumerations cater to in the following code:

'good
If TemparatureEnum.Hot Then
Cooler.Start
End If
'bad
If temp > 120 Then
Cooler.Start
End If

248

You might also like