Thursday, October 13, 2011

Enumerated Property


Add a class Customer.cs and write the following code: 
using System;
namespace oopsProject
{

    public enum cities { Bangalore, Hyderabad, Mumbai, Kolkata, NewDelhi, Chennai };
    public class Customer
    {
        int _custid;
        string _cname, _state;
        double _balance;
        bool _status;
        cities _city;
        public Customer(int _custid)
        {
            this._custid = _custid;


            //Get all other values from DB basing on given ID:
            _cname = "prabhu";
            _balance = 5000;
            _status = false ;
            _city = 0;
            _state = "karnataka";
            Country = "India";
                       
        }
        //Read only Property
        public int Custid
        {
            get { return _custid; }
        }
        //Read write Property
        public string Cname
        {
            get { return _cname; }
            set { _cname = value; }
        }
        //Read write Property
        public bool Status
        {
            get { return _status; }
            set { _status= value; }
        }
        //Read write Property with a condition
        public double Balance
        {
            get
            {
                if
                    (_status)
                    return _balance;
                else return 0;
            }
            set
            {
                if (value >= 500)
                    _balance = value;
            }
            }
            // enumerated property
       
        public cities city
        {
        get{return _city;}
        set{_city = value;}
        }
        // setting scope of property Accessor independently (2.0)
        public string state
        {
            get { return _state;}
            protected set { _state = value;}
        }
        //Automatic Properties 3.0
            public string Country
            {
            get;
            private set;
            }
       
           }
    }


Enumerated PropertyThese are   property which is defined with a list of constants to choose from. to define these type of properties first we need to declare all the constants under a type known as “enum” .An enum is a collection of constants, which should be declared as following.


Syntax – 
[<modifiers>] enum   <name> {<list of values >};


Eg:-

public enum Days {Monday, Tuesday, Wednesday, Thursday, Friday};

If this enum is used as a type for any property we call it as enumerated property.
- Declaring a variable of enum type
Days weekday = 0;
or
Days weekday = Days.Wednesday ;

Now for the variable weekday we can define a property as Following:
public Days weekday
{
get {return weekday;}
set { weekday = value;}

}
 
New Features to Properties:-

   <!--[if !supportLists]-->        <!--[endif]-->In C# 2.0, there are given an option to define a property setting the scope of each property Accessor independently.
Eg: State property we have defined under customer class.
   <!--[if !supportLists]-->        <!--[endif]-->In C# 3.0, we were given with an option automatic properties, which allows us to define a property without any variable declaration for the property, So set and get blocks doesn’t contain any code in them, but it is must to define both get and set blocks.
Eg: Country Property in above customer class.

Consuming the Properties:-
- Add a class TestCustomer.cs and write the following
using System;
namespace oopsProject
{
    class TestCustomer
    {
        static void Main()
        {
            Customer obj = new Customer(101);                                         
            Console.WriteLine(obj.Custid);
            //Assignment i not possible as property is read only
            //obj.Custid = 102;

            Console.WriteLine(obj.Cname);
            obj.Cname = "xyz";
            Console.WriteLine(obj.Cname);

            Console.WriteLine(obj.Status );
            //Balance will be zero here as status is inactive
            Console.WriteLine(obj.Balance);
            obj.Status = true;
            Console.WriteLine(obj.Status);
           
            // now it returns balance as status is active
           
            Console.WriteLine(obj.Balance);
            obj.Balance = 400; //assignment fails
           
            // here as it is less than 500

            obj.Balance = 400;
            Console.WriteLine(obj.Balance);

            // assignment succeeds as it is greater than 500

            obj.Balance = 600;
            Console.WriteLine(obj.Balance);
           
            Console.WriteLine(obj.city);
            obj.city = cities.Hyderabad;
            Console.WriteLine(obj.city);
            Console.WriteLine(obj.state);
            // Assigning value to the property is invalid as current
            //class is not a child class of customer
            //obj.state = "AP";
            Console.WriteLine(obj.Country);
            Console.ReadLine();

        }
    }
} 

Note – Just like we have Abstract method and Virtual method, we can define Property also as abstract and virtual. So that we can override them under Child class.

No comments: