VB.NET Enums | Trickcode

VB.NET Enum Declaration,The Essence of Enums,The Base Class of All Enums: System.Enum,Enums,VB.NET
Share it:




Introduction

I n .NET, Enums (short for Enumerations) are strongly typed constants. Enums adds readability to code and prevent accidental errors.


VB.NET Enum Declaration

In VB.NET, enums are declared with the following syntax.


Public Enum EnumName
    Member1
    Member2
    MemberN
End Enum

The EnumName is where the name of the enum is given and then any number of members, designated by Member1, Member2 and up to MemberN, are specified in the enum body. An enum must have at least one member - you can't have empty enums.

By default, enums use the Integer type to store their items. But you can change this. Your enums can use other types. The following are the types that your enums can use instead.


Integer (default)
UInteger
Byte
SByte
Short
UShort
Long
ULong


Following is an example of an enum that uses the Byte type for storage.


Public Enum EnumName As Byte
    Member1
    Member2
    MemberN
End Enum

Using a type other than Integer can have advantages. Using the Byte, for instance, saves space (Byte type takes less space than an Integer type).


The Essence of Enums


Let's imagine that you have written a top-notch application about car racing where you have a method called ChangeGear that changes gear.


Public Sub ChangeGear(ByVal gear As Integer)
    'Code that changes gear
End Sub
Typically, legal values that gear has are as follows.


First
Second
Third
Fourth
Fifth and
Reverse


Let's assume that the gear values are respectively represented by 1, 2, 3, 4, 5 and 0. Let's also assume that if the specified gear value passed to the ChangeGear method is not one of these then the method will not have an effect. Now, if the method is called with an illegal gear value, we have a bug - not a syntax bug but a behavior bug i.e. the application will behave unexpectedly. This can be hard to find.

We now enclose the gear values into an enum and see how we prevent the problem from occurring. The enum is given below.




Public Enum GearValues
    Reverse = 0
    First = 1
    Second = 2
    Third = 3
    Fourth = 4
    Fifth = 5
End Enum

We also have to change the signature of the ChangeGear method so it takes GearValues type as a parameter and not an Integer type.


Public Sub ChangeGear(ByVal gear As GearValues)
    'Code that changes gear    
End Sub

Now the ChangeGear method takes a GearValues enum as its parameter and the enum only defines legal values for gear. We, therefore, cannot make the mistake that we could before! This also makes the code more readable and type-safe. We could have defined the GearValues enum as follows.


Public Enum GearValues
    Reverse
    First
    Second
    Third
    Fourth
    Fifth
End Enum

The VB.NET compiler automatically assigns values to the members of enums starting from 0 and then incrementing it with 1. So the Reverse member will have a value of 0, the First member will have a value of 1, the Second member will have a value of 2 and so on. But you can also start with a value different from 0 starting with any member of the enum. Here is an example.


Public Enum GearValues
    Reverse
    First
    Second = 100
    Third
    Fourth
    Fifth
End Enum

In the above example, the Reverse and First members have values of 0 and 1 respectively. The value of the Second member is explicitly specified to be 100 and the values of Third, Fourth and Fifth are 101 (100 + 1), 102 (100 + 2) and 103 (100 + 3) respectively. If the values of the enum members do not matter then you do not need to specify any values at all as the compiler will take care of that for you.

The Base Class of All Enums: System.Enum

Any enum in the .NET framework implicitly inherits from the System. Enum. This base class gives you access yo to several useful shared methods. Here are most of them listed. GetNames returns an array of the names of items in an enum GetValues returns an array of the values of items in an enum GetUnderlyingType returns the type that is used to store the items of an enum Parse returns an enum type (boxed as an Object) given the name or value of an item in the enum IsDefined checks whether an enum member exists given its name or value






                                      Share this article with your friends
Share it:

VB.NET

Post A Comment:

0 comments: