Ketul Soni Ketul Soni - 3 months ago 15
ASP.NET (C#) Question

One class data use in another class data C#

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.

Answer

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 borrow you a hand)