C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Enumeration in .NET


Posted Date: 27 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: Sadha SivamMember Level: Gold    
Rating: 1 out of 5Points: 4



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






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Retrieving the name and parameters of current method using Reflection
Previous Resource: Tips in VB.NET - Tip #8
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use