Enumeration is a great user defined data type in C#.Net and VB.NET. It is very useful in code readability. It greatly increases the readability of code.
here is the example of the enumeration that contains the weekdays.
In C#.NET...
public enum weekdays
{
Sunday=1,
Monday=2,
Tuesday=3,
Wednesday=4,
Thursday=5,
Friday=6,
Saturday=7
}
In VB.NET...
public enum weekdays
Sunday=1,
Monday=2,
Tuesday=3,
Wednesday=4,
Thursday=5,
Friday=6,
Saturday=7,
end enum
here is the code that we can use in VB.NET
Imports System.ComponentModel
Imports System.Diagnostics
Dim enumWeekday As weekdays
Dim i As Integer
i=2
enumWeekDay=Ctype(i,weekdays)
debug.writeline(enumWeekdays.ToString())
Same way we can write in C#.NET
using System.ComponentModel;
using System.Diagnostics;
Weekdays enumWeekDays;
int i;
i=2;
enumWeekday=(Weekdays) i;
debug.writeline(enumWeekdays.ToString());
So you can see the it greatly increase readability also saves time to write code with switch case and if else
here is the example of the enumeration that contains the weekdays.
In C#.NET...
public enum weekdays
{
Sunday=1,
Monday=2,
Tuesday=3,
Wednesday=4,
Thursday=5,
Friday=6,
Saturday=7
}
In VB.NET...
public enum weekdays
Sunday=1,
Monday=2,
Tuesday=3,
Wednesday=4,
Thursday=5,
Friday=6,
Saturday=7,
end enum
here is the code that we can use in VB.NET
Imports System.ComponentModel
Imports System.Diagnostics
Dim enumWeekday As weekdays
Dim i As Integer
i=2
enumWeekDay=Ctype(i,weekdays)
debug.writeline(enumWeekdays.ToString())
Same way we can write in C#.NET
using System.ComponentModel;
using System.Diagnostics;
Weekdays enumWeekDays;
int i;
i=2;
enumWeekday=(Weekdays) i;
debug.writeline(enumWeekdays.ToString());
So you can see the it greatly increase readability also saves time to write code with switch case and if else
hi,
ReplyDeletei am hitesh. i have just started working on C#.net. I am having problem in using enum,list<>,interfaces...
so can you please guide me ?
thank you
//Enum Declaration
ReplyDeletepublic enum UserType:int
{
Admin=1,
Standard=2,
Premium=3
}
//Bind Enum to Dropdown
type.DataSource = Enum.GetValues(typeof(UserType));
type.DataBind();
//Retrieve List
objTable1.Type = (UserType)System.Enum.Parse(typeof(UserType), type.SelectedValue);
This comment has been removed by a blog administrator.
ReplyDeletepublic enum AdminInquiryStatus
ReplyDelete{
Approved = 1,
Rejected = 2,
Pending = 0
}