Introduction:
Enumeration is great user defined data type for both vb.net
andc#.it makes the code more readability by default enumeration type has int, but
user can change the type of enumeration
public enum xxxx
:type
{
}
By default Sequnce of
enumeration strat form 0
For ex:
public enum Currency
{
Euro,
Dollar,
Rupees,
Pound
}
The default values has set to euro is 0,dollar 1...etc so
the sequence always strat from 0
Its always hold the constant values..
Its common the question will arise in which situation we use
Enumeration in our project.
Normally in project where you want to use predefined values
in that cases enumeration would help get the predefined values stored in
enumeration.
For illustrate with one example..
In your project you need store currency value in dB, For an
every currency value you have an predefined one value would need to store in
table..
User can select the currency value, by upon the selection
you can get the stored value form the enum and update into table. In that case
there will be no chance you can store other than the predefined values..
Code:
public enum Currency
{
Euro = 1,
Dollar = 2,
Rupees = 3,
Ppund=4
}
//************WWW.dotnetcode.in**********
protected void Page_Load(object sender, EventArgs e)
{
//************WWW.dotnetcode.in**********
string str1,
str2,str3,str4;
//By comparing the
constant value we are getting the currency name imagine that value
//are sotred in
dropdownlist 1,2,3
str1 = (string)Enum.GetName(typeof(Currency), 1);
str2 = (string)Enum.GetName(typeof(Currency), 2);
str3 = (string)Enum.GetName(typeof(Currency), 3);
Currency ccy;
//Imagine the user
select the dollar cuurency you need to get the cuurency value from enum
ccy = Currency.Dollar;
string ccyname =
ccy.ToString();
int ccyvalues = (int)Currency.Dollar;
}
|
If this article helps you share this article with your
friends………
Keep reading……
0 comments:
Post a Comment