c# - How can the attributes of outer class can be accesed within the inner class in Composition Classes design? -
i totally unable access outer class attributes inside inner class ... if make object of outer class,, in inner class*which makes no sense in composition design* .. cant access them .. there way can access these outer class attributes ?
scenario there sports car constructed if customers want buy exists! ..
namespace composition{ public class customcar { #region attributes private string name; private string plateno; private double cost; private carcustomer _customer = new carcustomer(); #endregion #region properties public string name { { return name; } set { name = value; } } public double cost { { return cost; } set { cost = value; } } public string plateno { { return plateno; } set { plateno = value; } } public carcustomer customer { { return _customer; } set { _customer = value; } } #endregion #region methods public customcar() { console.writeline("i in custom car"); } public customcar(string s1, string pno, double c, string s2, double n, double bc) { this.name = s1; this.plateno = pno; this.cost = c; this.customer.name1 = s2; this.customer.nic1 = n; this.customer.bargaincost = bc; } public double finalcost() { if (this.customer.bargaincost < 10000) { double finalcost = (this.cost - this.customer.bargaincost); return finalcost; } else { return this.cost; } } public void show() { console.writeline(this.name + this.plateno + this.customer.name1 + this.customer.nic1); } #endregion public class carcustomer { private string name1; private double nic; private double bargaincost; public double bargaincost { { return bargaincost; } set { bargaincost = value; } } public double nic1 { { return nic; } set { nic = value; } } public string name1 { { return name1; } set { name1 = value; } } public carcustomer() { console.writeline("i have customer"); } public carcustomer(string n1, double i1, double bc) { this.name1 = n1; this.nic = i1; this.bargaincost = bc; } public void showcustomer() { console.writeline("customer name: " + name1); console.writeline("customer nic: " + nic1); } } } }
there nothing stopping having reference in carcustomer customcar object well. give 1 one reference between object. instaiate object in constructor of customcar
public customcar(arguments) { this.customer.customcar = this; }
or set in sets on property accessors you. try
public class customcar { private string name; private string plateno; private double cost; private carcustomer _customer = new carcustomer(); public string name { { return name; } set { name = value; } } public double cost { { return cost; } set { cost = value; } } public string plateno { { return plateno; } set { plateno = value; } } public carcustomer customer { { return _customer; } set { _customer = value; } } public customcar() { console.writeline("i in custom car"); } public customcar(string name, string pno, double c, string customername, double n, double bc) { this.name = name; this.plateno = pno; this.cost = c; this.customer.name1 = customername; this.customer.nic1 = n; this.customer.bargaincost = bc; this.customer.car = this; } public double finalcost() { if (this.customer.bargaincost < 10000) { double finalcost = (this.cost - this.customer.bargaincost); return finalcost; } else { return this.cost; } } public void show() { console.writeline(this.name + this.plateno + this.customer.name1 + this.customer.nic1); } } public class carcustomer { private string name1; private double nic; private double bargaincost; private customcar customer; public double bargaincost { { return bargaincost; } set { bargaincost = value; } } public double nic1 { { return nic; } set { nic = value; } } public string name1 { { return name1; } set { name1 = value; } } public customcar car { get{return customer;} set{customer = value;} } public carcustomer() { console.writeline("i have customer"); } public carcustomer(string n1, double i1, double bc) { this.name1 = n1; this.nic = i1; this.bargaincost = bc; } public void showcustomer() { console.writeline("customer name: " + name1); console.writeline("customer nic: " + nic1); } }
Comments
Post a Comment