3

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.

6
  • 2
    How do you want to use it? In a method? Have a unique instance per instance of the state_master? Commented Aug 10, 2016 at 10:43
  • in state_master Method. Commented Aug 10, 2016 at 10:45
  • 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. Commented Aug 10, 2016 at 10:47
  • XYZ is use because of another property is used. Commented Aug 10, 2016 at 10:52
  • any other way like interface??? Commented Aug 10, 2016 at 10:52

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

you can change your code so that state_master inherit country_master

public class state_master: country_master 

4 Comments

public class state_master : XYZ is compulsory write because of XYZ properties and its methods used in the state_master class.
Since country_master inherits XYZ, then state_master would inherits both XYZ and country_master
@Ehsan.Saradar have you tried this out? to inherit from 2 base classes?
yes, state_master inherits country_master and country_master inherits XYZ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.