Enumeration Introduction In this section of article lets see something on Enumeration. Before getting into Enumeration lets see the literal meaning of the Enumeration - "name (things on a list) one by one". I hope you could have guessed what this enumeration can be in Programming. Yes, it is a type which can hold a set of values. Say for example, if you see Marital Status of the Employee, you can have only some specific data, say either he can Single, Married or divorced. Other than this there can't be anything else. So this marital status should hold any of this data only, it should not accept any other data. This can be achieved using the Enumeration. Definition Enumerations represents integral types that have a limited set of allowed values. (Taken from VB.Net Class Design by Andy Olsen, Damon Allison, and James Speer). Defining an Enumeration in VB.NET Defining an Enumeration in VB.NET is very simple, it is more or less like defining a structure. To declare an Enumeration, we use the Enum Keyword and specify names to represent the allowable values. Syntax
Enum '................. '................. Enumeration keys '................. End Enum
Example Lets see an example of creating an Enumeration to store the 12 months.
Public Enum EnumMonth Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec End Enum
Declaring an object for the Enumeration Declaring an object is very similar to declaring an object for any other value type. Example: Lets declare a variable for the Enumeration EnumMonth.
Dim mon as EnumMonth = EnumMonth.Jan
Note:
In the .Net Framework, enumerations are treated as a special kind of value type. This is appropriate, because enumerations map to integer-based data types internally.
Also, an enumeration should have at least one member.
Enumerations are more flexible, we can also specify an underlying integer data type for the enumeration (Byte, Short, Integer, or Long).
Syntax
Enum {Enum name} as {Byte, Short, Integer or Long} ................................. ................................. Members ................................. ................................. End Enum
dvantages of Enumeration
- Will improve the readability of the program. We can give some meaningful names to represent the allowable values.
- We cannot assign the value other than its members. Ie., Enumerations are strongly typed. This helps avoid programming errors that might arise if we use raw integer variables.
Please send me your valuable comments and feedback to sadhasivam1981@yahoo.com. Visit http://sadhasivam.t35.com
|
No responses found. Be the first to respond and make money from revenue sharing program.
|