How to call classes from one Project in another Project?

30,671

Solution 1

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.

Solution 2

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

Solution 3

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

How to add assembly reference

Share:
30,671
13driver
Author by

13driver

Updated on July 09, 2022

Comments

  • 13driver
    13driver almost 2 years

    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 ?