welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, June 6, 2012

How the enumeration used in C# or in project

Enumeration is great user defined data type for both 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
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..


  public enum Currency
            Euro = 1,
            Dollar = 2,
            Rupees = 3,
    protected void Page_Load(object sender, EventArgs e)
        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……


Post a Comment