In the same solution, I have two projects: P1 and P2. How can I make use of a class of P1 in P2?
All too often a new developer asks this simple question which is a common problem specifically with Visual Studio IDE's. Few people answer the specific question and often critique the question or give "guesses" for solutions which don't answer the common problems. The first common problem is the IDE leads you to create new projects rather than add new files (.java, .py, .cpp, .c) to the existing solution (by default it creates a new solution) unless you change the project name and add to the current solution. This problem occurs for Python, java, c#, C++ and C project folders.
The new developer selecting "new>project>project name and changing the solution directory to "use same solution" still creates a new "project" in the same solution space, but not in the same directory space as the current user interface file or command line file which still leads to problems with "package not found" errors when building and running the project or solution. This is why the above coding suggestions to importing packages, classes, methods and functions only work (and thus don't answer the question) when the "library" file or "separate behavior" file is not only in the same solution directory path, but also in the same "user interface" or "command shell" application directory space. This does does not happen when you add another project using the new>project>project type commands of the IDE. The problem here is the new project is stored in a different directory than the existing Client or User interface code files. To create a new "file" in the same project space rather than new project the beginner needs to do the following that Microsoft won't do for you and even misleads you away from the intuitively obvious by default.
NOW the code recommendations to import libraries or using namespaces will work as described in the comments above and you don't have to change path statements or change solutions paths and solution names that Microsoft won't let you change easily (i.e. you can change the filenames or project names but the IDE won't automatically change the project path or the solution path names).
The following is a Python example but works similar for C#, java, or C/C++ using the includes, namespaces or using code commands appropriate to each language to find code in other classes/projects in the SAME DIRECTORY SPACE.
The application file "hello world" importing from other code files in the same directory.
Note the python white space delimiters are not going to space correctly in this stackoverflow comment editor:
print ("test")
from CIXMPythonFacade import ClassA
c1=ClassA
c1.methodA()
from CIXMPythonFacade import functionA
functionA()
class ClassName(object):
def __init__(object, parameter):
object.parameter = value
The library file or "façade" file containing classes, methods or functions you want to import.
class class1(object):
"""description of class"""
class ClassA(object):
print ("test2")
def methodA():
print ("test3")
def functionA ():
print ("test4")
return (0)
pass
NOW how do you actually solve the mess that the IDE leads you into? To import code from another file in the same directory space you add a reference to it.
OK so now that you have this problem solved, how do you really link two separate projects together in the same solution space?
Microsoft really, really needs to fix these problem so you can intuitively create what most people want to create as new files in the same directories and remove solutions by selecting them and deleting them from the IDE. Beginners get so frustrated with directory path statements so flexible for seasoned developers, but so unfair to new developers in their defaults.
Hope this really helps you new guys and stops seasoned developers from giving you the wrong answers that don't work for you. They assume you already understand path statements and just want to type the right code...which is also why the tunnel in on trying to correct your code but does not help you fix the problem. This is probably the most common problem continually described on stackoverflow with wrong answers that don't work for new programmers.
If you have two projects in one solution folder.Just add the Reference of the Project into another.using the Namespace you can get the classes. While Creating the object for that the requried class. Call the Method which you want.
FirstProject:
class FirstClass()
{
public string Name()
{
return "James";
}
}
Here add reference to the Second Project
SecondProject:
class SeccondClass
{
FirstProject.FirstClass obj=new FirstProject.FirstClass();
obj.Name();
}
The first step is to make P2 reference P1 by doing the following
Next you'll need to make sure that the classes in P1 are accessible to P2. The easiest way is to make them public
.
public class MyType { ... }
Now you should be able to use them in P2 via their fully qualified name. Assuming the namespace of P1 is Project1 then the following would work
Project1.MyType obj = new Project1.MyType();
The preferred way though is to add a using for Project1
so you can use the types without qualification
using Project1;
...
public void Example() {
MyType obj = new MyType();
}
Paul Ruane is correct, I have just tried myself building the project. I just made a whole SLN to test if it worked.
I made this in VC# VS2008
<< ( Just helping other people that read this aswell with () comments )
Step1:
Make solution called DoubleProject
Step2:
Make Project in solution named DoubleProjectTwo (to do this select the solution file, right click --> Add --> New Project)
I now have two project in the same solution
Step3:
As Paul Ruane stated. go to references in the solution explorer (if closed it's in the view tab of the compiler). DoubleProjectTwo is the one needing functions/methods of DoubleProject so in DoubleProjectTwo right mouse reference there --> Add --> Projects --> DoubleProject.
Step4:
Include the directive for the namespace:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DoubleProject; <------------------------------------------
namespace DoubleProjectTwo
{
class ClassB
{
public string textB = "I am in Class B Project Two";
ClassA classA = new ClassA();
public void read()
{
textB = classA.read();
}
}
}
Step5:
Make something show me proof of results:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoubleProject
{
public class ClassA //<---------- PUBLIC class
{
private const string textA = "I am in Class A Project One";
public string read()
{
return textA;
}
}
}
The main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DoubleProjectTwo; //<----- to use ClassB in the main
namespace DoubleProject
{
class Program
{
static void Main(string[] args)
{
ClassB foo = new ClassB();
Console.WriteLine(foo.textB);
Console.ReadLine();
}
}
}
That SHOULD do the trick
Hope this helps
EDIT::: whoops forgot the method call to actually change the string , don't do the same :)
Say your class in project 2 is called MyClass.
Obviously, first reference your project 2 under references in project 1 then
using namespaceOfProject2;
// for the class calling bit:
namespaceOfProject2.MyClass project2Class = new namespaceOfProject2.MyClass();
so whenever you want to reference that class you type project2Class. Plus make sure that class is public too.
I had an issue with different target frameworks.
I was doing everything right, but just couldn't use the reference in P2. After I set the same target framework for P1 and P2, it worked like a charm.
Hope it will help someone