简体   繁体   中英

Accessing base class variables C#

I have base class

public class X
{
   public int Id;
   public int i;
}

and derived class

public class Y : X
{

//other variables

}

I have 3 projects in my solution. Class X is in project A, class Y in project B, and in project CI am trying to do:

Y newY = new Y();
newY. // this is where no variables from base class are showed (no Id or i)

If I reference project A from project C, it works. But can I do it without referencing project A from C?

Existing references are : B -> A and C -> B

Project B does required reference Project A as class Y is derived from class X and some of the parent functionalities are written in Project A only.

so adding reference of only ProjectB (not ProjectA ) in your mail project, and trying to call functionalities of ProjectB will surely fail in compile time.

like below.

//of Project A
public class X
{
    public string XStr;
    public void MethodOfX()
    {
        
    }
}

and

// of Prject B
public class Y : X
{
    public string YStr;
    public void MethodOfY()
    {
        //values form Project A is available
        // as assembly reference has added here
        YStr = XStr;
        MethodOfX();
    }
}

you can see, here you can access properties of class x too. as reference of ProjectA has been added in ProjectB .

and in main class. by adding reference of ProjectB only, if we try following.

    static void Main(string[] args)
    {
        Y y = new Y();
        y.MethodOfY();
    }

on Y y = new Y() we will get this error on compile,

The type 'ProjectA.X' is defined in an assembly that is not referenced. You must add a reference to assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Here error is self explanatory. it simply state that for doing Y y = new Y() you will required to deal with ProjectA.X (here in our case it is because we are inheriting it) and assembly which is having definition of ProjectA.X is not yet referenced in main project.

have a look in this question

As here we are having Y inheriting X we need to add reference of ProjectA too in our main project because in this case Y is holding some functionalities which is defined in ProjectA .

And as we haven't added ProjectA 's reference in our main project where we are trying to create object of Y, that object will not able to expose Y's those functionalities and members which are defined in ProjectA


but if we change our code like below.

//of Project A
public class X
{
    public string XStr;
    public void MethodOfX()
    {
        Console.WriteLine("Method of x");
    }
}

and

// of Prject B
public class Y 
{
    public string YStr;
    public void MethodOfY()
    {
        Console.WriteLine("Method of Y");
        X x = new X();
        //values form Project A is available
        // as assembly reference has added here
        YStr = x.XStr;
        x.MethodOfX();
    }
}

Note that now Y is not inheriting X, but simply creates an instance of X and using it.

static void Main(string[] args)
    {
        try
        {
            Y y = new Y();
            y.MethodOfY();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        Console.Read();
    }

in our main project even after adding reference of only ProjectB and not ProjectA , above code will not cause any exception. because here while creating object of Y main project doesn't need to deal with ProjectA as all functionlities and members of Y are in projectB only.

And while calling the method MethodOfY() even if that method internally calls method of x too, this calls will be handled if dll of ProjectA is present in Application's run path (if you are debugging, usually in Debug folder).

Note: if dll of ProjectA is not there by any circumstances it will throw an exeption while calling MethodOfY() saying that it cannot load ProjectA dll (of course becuase it was not present there)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM