Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I refer to a project from another one in c#?

People also ask

How do you reference a class from another project?

You will need to right click your project and select add -> Reference... -> Projects and tick the project you would like to reference. The result of this should show the referenced project under the references section of your project tree.

How do you call another project from a project in the same solution?

Step 1: Make a reference from Project A to B. Step 2: Make Project B's Program. cs Public by adding the word "public" in front of "class Program". Step 3) Make a public method returnPath().


First, you need to add a reference to Project2 in Project1.

If you go to Project1 -> References -> Add Reference, you should see an option to add projects in solutions and add project2.

Once you add a reference, to call a class Foo in namespace Name1.Name2, you can use the class as

Name1.Name2.Foo foo = new Name1.Name2.Foo(...);

or if you would like to avoid typing, you could add the using statement near the top of the file

using Name1.Name2;

and can now reference the class using just Foo, like

Foo foo = new Foo(...);

Note, you will have figure out the namespaces in Project2. Just using the name Project2 won't work. Look at the file which contains the declaration of the class you want to use and look for the namespace definition.

So if you see something as

namespace Name1.Name2 {


    class Bar {
        // Blah
    }

    // Notice the word public here.
    public class Foo {
        // Blah
    }
}

Name1.Name2 is the namespace for Foo and that is what you need to use.

Also note that you will likely need to have the public access modifier for the class which you want to use in Project1. For example in the above scenario, you should be able to access class Foo, but not class Bar.

This page will help you understand namespaces: http://www.csharp-station.com/tutorials/lesson06.aspx


I just had this very same problem. I triple checked everything, and references were added, project was in the same solution, everything set. I also know my namespaces, I even got code completion but when building, I got the same error that the namespace cannot be found.

It took me one hour to find this out: The project I added, that didn't find the respective namespace, was set to a different .NET target framework. I changed my solution to the "full" .NET Framework 4 instead of the Client Profile 4, but the new project I added was set to the Client Profile again.

Instead of giving me a proper warning about the bad configuration, it claimed that namespaces could't be found.

Hope this helps someone, and saves you the time that I just wasted.


You have to specify the "path" to the code you're trying to call. You accomplish this by either

1) Specify the namespace of the second project/class you wish to use, typically at the top of your code file.

using MySecondProject;

var foo = new ClassFromSecondProject();

2) Explicitly specifying the name of the class you wish to use, including its namespace

//do stuff
var foo = new MySecondProject.ClassFromSecondProject();
//do more stuff

You just need to add a reference in Project1 to Project 2. You do this by right-clicking the References folder from the solution explorer pane then you can use the Browse option to find Project2. Or if it is already added to the solution you can just use the Projects tab.

Just to clear this up for you. Adding a project to the Solution is not the same as adding a reference. Open up Project2 in Visual Studio. Then either add Project1 to the solution aswell or right click on the References folder in Project2 and add a reference to Project1. To ensure you have properly added a reference expand the references folder and verify you can see Project1 in the list.

Example

Create a new console application and call it MyApplication. Then right click on the Solution and select the Add New Project option and create a new library project and call it MyLib. At this point you have simply added 2 projects to the 1 solution, no references between each project have been created.

Right click the References folder under the MyApplication project and select Add Reference.... As MyLib is already part of the solution you can go to the Projects tab and select MyLib from the list which creates a new reference to this project in MyApplication. If it is not part of the solution you can use the Browse tab and find the project via explorer.

So at this point we have established a reference inside MyApplication to MyLib. So in order to use the classes from MyLib inside MyApplication we can either declare a using for the project inside the unit or we can use the full path directly e.g.

// main code file in MyApplication

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyLib;  // This will allow me to access the classes inside MyLib directly

namespace PdfPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
             // if we have declared the namespace at the top, we can do:
             MyLibClass cls = new MyLibClass();
             // or if you don't want to add the namespace at the top we have to do:
             MyLib.MyLibClass cls = new MyLib.MyLibClass();
        }
    }
}

Hope that clears it up a bit for you.


Select the project you will be using (project1 for example), right click it in the solution explorer and click "Add reference".

You'll be able to add a reference to the other solution from there (project2).

All you have to do then is add a using statement in your main project (project1) and you'll be able to access it normally.