views:

104

answers:

6

In the same solution, i have two projects P1 and P2. how can i make use of a class of P1 in P2 ?

+3  A: 

Simply add reference to P1 from P2

Nissim
This won't work if the class is non-public or if P2 is a friend of P1.
John
In this case you have 3 options:1) Combine the 2 projects2) Make the class internal - and add P2 as "InternalsVisibleTo" attribute (more information here: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx).3) If none of the above can be done - you will have to use reflection to instantinate and invoke class P1 from P2
Nissim
+1  A: 

In project P1 make the class public (if it isn't already). Then add a project reference (rather than a file reference, a mistake I've come across occasionally) to P2. Add a using statement in P2 at the correct place and start using the class from P1.

(To mention this: The alternative to making the class public would be to make P2 a friend to P1. This is, however, unlikely to be the answer you are after as it would have some consequences. So stick with the above suggestion.)

John
+3  A: 
  1. In the 'Solution Explorer' tree, expand the P2 project and then right-click the project and select 'Add Reference' from the menu.
  2. On the 'Add Reference' dialog, select the 'Projects' tab and select your P1 project.
  3. If you are using namespaces then you will need to import the namespaces for your P1 types by adding 'using' statements to your files in P2.

Note that the types in P1 that you wish to access directly must have a sufficient access level: typically this means they must be made public.

Paul Ruane
+2  A: 

The first step is to make P2 reference P1 by doing the following

  • Right Click on the project and select "Add Reference"
  • Go to the Projects Tab
  • Select P1 and hit OK

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();
}
JaredPar
A: 

If u have two projects in one solution folder.Just add the Reference of the Project into another.using the Namespace u can get the classes.While Creating the object for that the requried class.Call the Method which u 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(); }

programmer4programming
+1  A: 

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

Proclyon