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