0

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 C I 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

Jack
  • 77
  • 2
  • 9
  • he said each class in separate DLL and use different DLL to use Y class and there is no intellisense – Vince May 28 '18 at 23:33
  • 4
    you'll absolutely need a c=>a reference; with nuget package references it may now be transitive ; anything else, you'll need to be explicit – Marc Gravell May 28 '18 at 23:37
  • 3
    If you want to access `Class X` in `Project A` from `Project C` you _must_ have a reference from `Project C -> Project A`. If you have `Project B -> Project A` and `Project C -> Project B`, Visual Studio may have added `Project C -> Project A` behind the scenes. – Andrew Williamson May 28 '18 at 23:38
  • Can you explain why is that? Because as far as I can see all I am using in `Project C` is a Class from `Project B` and I have referenced `Project B` from `Project C`? – Jack May 28 '18 at 23:48
  • 1
    Because the definition of X, you could say its metadata, is on `Project A`. – Camilo Terevinto May 28 '18 at 23:52

1 Answers1

1

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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amit
  • 1,821
  • 1
  • 17
  • 30