How to call classes from one Project in another Project?

孤者浪人 提交于 2020-01-14 14:20:31

问题


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 ?


回答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.




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




回答3:


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

How to add assembly reference




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/11383181/how-to-call-classes-from-one-project-in-another-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!