Introduction
The idea is to explain Enumeration with some live example. Have you seen an inbuilt dialog box with “No To All” button. While I was working in one of my project I needed a dialog box having “No”, “No To All”, Yes , Yes To All etc buttons. We can easily create such dialog box using a winform and C# enumeration.
Enumeration
Enums are value types that are created on stack but not on the heap. They require fewer resources from both the processor and the memory on which the application is running. You don'nt have to use new to create an enum type.
public enum Seasons { winter, summer, autumn}
An Enum is a named constant whose underlying type is any integral type except char. If no underlying type declared, Int32 is used. C# typically provide syntax to declare an enumeration that consists of a set of named constants and their values.
public enum Seasons { Winter = 1, summer = 2, autumn = 3 }
The assignment of numeric values is optional.By default the number begins with zero, but this may be overriden. Dot notation is used to access individual enumerators.
Enumerator allows code to look a lot cleaner and easier to read. Enum help us to get rid of numbers which have a purpose within a module of code, but make the code harder to read.
Let’s discuss some of the disadvantage of using Constants or number directly.
public void ShowSeason(int option)
{
switch(option)
{
case 1 :
MessageBox.Show("Winter");
break;
case 2 :
MessageBox.Show("summer");
break;
case 3 :
MessageBox.Show("Autumn");
break;
}
}
For displaying winter we have to call ShowSeason like this
ShowSeason(1);
A Few problems with this kind of code:
- Not readable : If we see the code, its really difficult to get what 1, 2 and 3 are for?
- Not Typesafe : These are not type safe. Instead of passing 1, we can pass number like -999, which is meaningless in our context.
There are many more disadvantages. Lets convert the above code usning Enum and see how it solves the above problem.
Declare Enum :
Public enum Seasons {Winterm, Summer, Autumn}
Use above enum in function "ShowSeason"
public void ShowSeason(Seasons s)
{
Switch(s)
{
case Seasons.winter :
MessageBox.Show("Winter");
break;
case Seasons.Summer :
MessageBox.Show("Summer");
break;
case Seasons.Autumn :
MessageBox.Show("Autumn");
break;
}
}
Call the function like this :
ShowFunction(Seasons.Winter);
Advantage :
- More Readable
- Fully type safe.
Let's make a Reusable Dialog Box Using Enumeration
Open a class library and drag a form(MyDialog.cs). Take few buttons on the form and named them as btnYes, btnNo, btnNoToAll etc. Also drag a label (lblMsg) on it to display message. Write following codes in the code behind of MyDialog.cs.
Public partial class MyDialog : Form
{
public enum Clicked{Yes,No, NoToAll}
private Clicked ClickedResult = Clicked.No;
Public Clicked prtClickedResult
{
get { return ClickedResult ;}
}
Public MyDialog(string strMsg)
{
InitializeComponent();
lblMsg.Text = strMsg;
}
Private void btnYes_Clicked(object sender, EventArgs e)
{
ClickedResult = Clicked.Yes;
this.close();
}
private void btnNoToAll_Clicked(object sender,EventArgs e)
{
ClickedResult = Clicked.NoToAll;
this.Close();
}
}
Most of the things are self explanatory. Here, whenever the user clicks any button, the enumeration value of the corresponding button is assigned to "ClickedResult" which will be latter accessed by our appliaction through the public property "prtClickedResult".
For using this, built the class library and include the dll to your application and write following codes :
string strMsg = "Are you sure you want to delete this file"
MyDialogs.MyDialog obj = new MyDialogs.MyDialog(strMsg);
obj.ShowDialog();
MyDialogs.MyDialog.Clicked objEnum = obj.prtClickedResult;
if(objEnum == MyDialogs.MyDialog.Clicked.Yes)
{
}
else if (objEnum == MyDialogs.MyDialog.Clicked.NoToAll)
{
}
.
Happy Coding !!!!