property overriding not working in C#?

Here i have written Special Customer class which is child class and i have overridden Property Cname where i want Special Customer can change the Name only if they Cname="Special" but currently it's not happening it is going to base Cname property which is checking balance (which i don't want for Special Customer class) and pls let me know if i am achieving Runtime Polymorphism by this code

  class Customer
    {
       protected int _cid,_bal,_status;
        protected string _cnmae;

        public Customer(int _cid,int _bal,int _status,string _cname)
        {
            this._cid = _cid;
            this._bal = _bal;
            this._cnmae = _cname;
            this._status = _status;
         }
        public int Cid
        { //read only property
          get
            {return _cid;}

           }
        public virtual string Cname
        {
            get
            {return _cnmae;}
            set
            {
                if (_status != 0 & _bal >= 500)
                {_cnmae = value;}
            }
        }
        public int Bal
        {
            get
            {return _bal;}
            set
            {
                if (_status != 0 & value >= 500)
                { _bal = value;}
            }
        }
        public int Status
        {
            get{ return _status;}
            set
            {_status = value;}
        }
       public  virtual void display()
        {
          Console.WriteLine("id={0} and name={1} and balance={2} and status={3}", _cid, _cnmae,_bal,_status);
        }
  }

 class Specialcustomer:Customer
    {
        public Specialcustomer(int _cid, int _bal, int _status, string _cname) :base( _cid, _bal, _status,_cname)
        {
        }
        public override string Cname
        {
            get
            {return base.Cname}
          set
            {if (value == "SPECIAL")
                {
                    base.Cname = value;
                }
            }
        }

        public override void display()
        {
          Console.WriteLine("id={0} and name={1} and balance={2} and status={3}", _cid, _cnmae, _bal, _status);
        }
}
 class Program
    {        static void Main(string[] args)
        {
            Customer C1 = new Specialcustomer(10, 400, 1, "BOND");
            C1.display();
            C1.Cname = "SPECIAL";
            C1.display();
            Console.ReadKey();

        }
    }
Jon Skeet
people
quotationmark

Your code is working already - it's calling the SpecialCustomer.Cname setter, as you can easily tell either by setting a breakpoint in it or adding some logging. (I just added a Console.WriteLine statement in the setter.)

However, it's not changing the value of Cname because of the condition in the Customer.Cname setter:

set
{
    if (_status != 0 & _bal >= 500)
    {
        _cnmae = value;
    }
}

If you change your code so that the balance of the customer is (say) 600 instead of 400, then Cname changes to SPECIAL as you'd expect.

If you want SpecialCustomer to change the name unconditionally if the value provided is SPECIAL, you'll need to add that functionality into the base class, e.g.

class Customer
{
    public virtual string Cname
    {
        get { return _cnmae; }
        set
        {
            if (_status != 0 & _bal >= 500)
            {
                SetCnameUnconditionally(value);
            }
        }
    }

    // This method allows subclasses to bypass the conditions
    // in the normal setter
    protected void SetCnameUnconditionally(string value)
    {
        _cnmae = value;
    }
}

class SpecialClass
{
    public override string Cname
    {
        get { return base.Cname; }
        set
        {
            if (value == "SPECIAL")
            {
                SetCnameUnconditionally(value);
            }
        }
    }
}

people

See more on this question at Stackoverflow