Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
here i have created Specialcustomer class in which i want it to change the name if cname="special"
PLEASE LET ME KNOW IF I AM DOING RUN TIME POLYMORPHISM BY THIS ?
pls help i am new to C#


   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,Cname,Bal,Status);
            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()
        {
            //base.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();
         }
    }


What I have tried:

Quote:
i have created Specialcustomer class in which i want it to change the name if cname="special"
PLEASE LET ME KNOW IF I AM DOING RUN TIME POLYMORPHISM BY THIS ?
pls help i am new to C#
Posted
Updated 9-Apr-17 7:24am
v3

You need a base property or method that sets the _cnmae field without performing any checks. If you don't want it visible to the importers of the classes, make it protected. Then, in each of the class's CName property, call this protected method to set the value if or when the conditions are suitable. i.e.

C#
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;
            }
            
        }

        protected void SetCname(string value)
        {
            _cnmae = value;
        }

        public virtual string Cname
        {
            get
            {
                return _cnmae;
            }
            set
            {
                if (_status != 0 & _bal >= 500)
                {
                    SetCname(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,Cname,Bal,Status);
            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.SetCname(value);                }
            }
        }
 
        public override void display()
        {
            //base.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();
         }
    }
 
Share this answer
 
Change base class property to below

public virtual string Cname
       {
           get
           {
               return _cnmae;
           }
           set
           {
               if (value == "Champ"|| (_status != 0 && _bal >= 500))
               {
                   _cnmae = value;
               }
           }
       }
 
Share this answer
 
Comments
Akhil Jain 9-Apr-17 2:53am    
can this be solved by not changing the base Class Code !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900