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
As stated by others, you need to add the (existing) projects to the solution. Then you need to add a reference to them under Solution -> Projects. They should now show up with their namespaces in the Object Browser.
There is one more thing you need to make sure, which took me some experimentation to find out. Whenever I added my second project, the first one stopped working, and the error message was the same as when the project misses a reference ("The type or namespace name 'Ber' could not be found (are you missing a using directive or an assembly reference?)"). The problem was that both projects had the same default Assembly name ("Class Library") in project Properties -> Application. So give them unique names. I use the same as the Default namespace.
I don't have enough rep to upvote Tom's answer but it helped me greatly. In addition, you must select the same subversion of the .NET Framework (in addition ot it being the same full/core/standard etc). I had 4.5 specified in a testing project and got this error, changing it to 4.6.1 (the same as the others) fixes this issue.
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.