7

Excuse the incredibly silly question but im new to C# . I just can’t figure out how to use classes from one Project in another Project.

Lets say I want to take a string from Project1 to Project2 and have Project2 print said string .

I reference Project2 from Project1 with the “add reference” menu , then I add “using Project2” and then I write this to trying and call "print" from "ClassA" in "Project2".

        Project2.ClassA Classa = new Project2.ClassA();
        Console.WriteLine(Classa.print);

but all i get are Error messages .

so can anyone please give a step by step explanation of EXACTLY why I need to do ?

Tilak
  • 30,108
  • 19
  • 83
  • 131
13driver
  • 237
  • 2
  • 5
  • 10
  • 1
    As well as giving the error messages it is usually necessary to give all the code - in this instance the code where you define ClassA and the declaration of the `print` field. Without that sort of detail people will just be guessing what your problem is. – David Hall Jul 08 '12 at 12:41
  • In not asking for anyone to fix my code im asking for a step by step explanation of how to call one project from another . – 13driver Jul 08 '12 at 14:36

4 Answers4

10

Follow these instructions:

  1. In the Solutions Explorer, right click on the project that you need to refer from.

  2. Select "Add Reference". (latter versions i.e. beyond 2015; not exactly sure though; it should be "Add->Reference". Right click on References and select Add Reference also will do.)

  3. Reference Manager - ProjectName dialog will appear.

  4. In the left pane, expand Projects menu. That will populate a list of existing projects in the middle of the dialog.

  5. Tick the checkbox before each project that you need to refer to.

  6. Press OK.

Once done, you can use all the public and protected (given you are inheriting from an existing one) in the project that you have referred to. Make sure you add using NameSpace.Class on the imports list.

Romeo Sierra
  • 1,666
  • 1
  • 17
  • 35
Khalid
  • 603
  • 9
  • 22
  • 2
    Oh, my. I want to clean up your English and formatting, but I'm having a hard time working out exactly what you're trying to say. "Window Appear which show some option of left side", in particular, is pretty much incomprehensible. – user1618143 Dec 06 '13 at 19:54
5

When you reference the Class from Project2 it is probably in a different namespace.

Add the namespace in the top of your class where you are going to use is (the using statements) or, go with your cursor over the Project2.Class and let Visual Studio do it for you :-)

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • I added “using Project2;” and I referenced it with “project” tab in the “add reference” menu still no luck – 13driver Jul 08 '12 at 14:34
3

Make Classa public, and add assembly reference/project reference of Project1 to Project2.

How to add assembly reference

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • I did all this still no luck =/ Is there a guide or an example somewhere ? – 13driver Jul 08 '12 at 15:20
  • For this command “ClassA Classa = new.ClassA();“ I keep getting two copies of “The type or namespace name 'ClassA' does not exist in the namespace 'DoubleProject1' (are you missing an assembly reference?)” – 13driver Jul 08 '12 at 18:41
  • Add reference of DoubleProject1, make sure ClassA is public – Tilak Jul 09 '12 at 03:19
0

Go to the project Say P2, expand it go to references right click add reference. Go to project Tab then browse for the Project P1 and click add. After that you would need a using statement for that project to get the files. Click on rebuild solution it will give you the output.