3
votes

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; }
        }
}
  • Now, I want to use country_name in my state_master class how it is possible?

Thank You.

3
How do you want to use it? In a method? Have a unique instance per instance of the state_master?doctorlove
in state_master Method.Ketul Soni
Bro, the class state_master can use only XYE properties, if you wanna use country_name you can have an instanace of country_master as a property in your state_master class, or maybe move country_name to XYZ class.Badro Niaimi
XYZ is use because of another property is used.Ketul Soni
any other way like interface???Ketul Soni

3 Answers

3
votes

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)

2
votes

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);
0
votes

you can change your code so that state_master inherit country_master

public class state_master: country_master