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.
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
{
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, _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()
{
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();
}
}