views:

328

answers:

5

I added a project, Project2, to my solution. It already had another project lets say Project 1. How can I call classes and methods from project2 into project1?

What I did:

I have Project 1 and its solution. I added Project 2 to Project 1's solution. Project 1 and Project 2 both have a namespace named Correction. Now, I called using Correction. However, in Project 1 typing Project2 gives me an error since its claiming that it does not know what it is.

I also added project 2 as a reference.

thanks for all the answers. i dont know what i am doing wrong

+16  A: 

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

Moron
Yes I have done that, thanks :)But how do I call it?
C.
@C., what do you mean, how do you "call it"? Once you reference it you now have access to that namespaces classes and methods. Do with them what you want.
Mithrax
@C: I have edited the answer to provide more information, read it and see if it helps.
Moron
A: 

You need to add Project2 to the references of the Project1. Now you can access the classes from Project2. Don't forget to add the correct namespace!

Vlad
its doesnt find the correct namespace :/
C.
A: 

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.

James
I added the project by using ADD EXISTING PROJECT to the solution...but its not letting me call it :/
C.
You have only added the project to the `solution` this is not the same as adding a `reference` from one project to another. As I mentioned before, you need to right click the `references` folder inside Project2 and add a reference to Project1.
James
+1  A: 

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.

Jamie Keeling
it wont let me just use the using project2 statement :/
C.
You have to create an object from it, see bakasans answer as he explains it more clearly than I ever could =]
Jamie Keeling
+2  A: 

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
bakasan