Reputation: 85
I have following two class which is already inherited to XYZ
Country Class
public class country_master : XYZ { private string _id; public string id { get { return _id; } set { _id = value; } } private string _country_code; public string country_code { get { return _country_code; } set { _country_code = value; } } private string _country_name; public string country_name { get { return _country_name; } set { _country_name = value; } } }
State Class
public class state_master: XYZ { private string _id; public string id { get { return _id; } set { _id = value; } } private string _state_code; public string state_code { get { return _state_code; } set { _state_code= value; } } private string _state_name; public string state_name { get { return _state_name; } set { _state_name= value; } } }
country_name
in my state_master
class how it is possible?Thank You.
Upvotes: 3
Views: 233
Reputation: 23732
You would need a variable of type country_master
in your state_master
class.
Then you can access the property country_name
.
Crossinheritance is not possible unfortunately. (If you have a brother you cannot just use his hands, although you inherit from the same parent. You would need your brother in person.)
Example:
public class state_master: XYZ
{
private country_master _cm;
public country_master cm
{
get { return _cm; }
set { _cm = value; }
}
public void state_method()
{
this.cm = new country_master();
this.cm.country_name;
}
}
Another possibility would be of course to pass the variable from outside at the call to the method
public void state_method(string country_name)
{
// use country name
}
calling site:
state_master sm = new state_master();
country_master csm = new country_master();
sm.state_method(cm.countr_name);
(Now you are asking your brother to lend you a hand)
Upvotes: 3
Reputation: 348
There's more than one way to skin a cat.
You can create a new instance of country_master:
public class state_master: XYZ
{
private country_master CountryMaster;
// Class constructor
public state_master()
{
CountryMaster = new country_master();
}
private string _id;
...
or pass an existing instance of country_master to the constructor:
public class state_master: XYZ
{
private country_master CountryMaster;
// Class constructor
public state_master(country_master cm)
{
CountryMaster = cm;
}
private string _id;
...
and call it with
country_master MyCountry = new country_master();
state_master MyState = new state_master(MyCountry);
Upvotes: 2
Reputation: 704
you can change your code so that state_master inherit country_master
public class state_master: country_master
Upvotes: 0