0

Very new to C#/VS and this is vexing me - there are a lot of similar questions on SO, but none seem specific enough to help me!

I'd like to use a class from ProjectA in another class (eg ProjectB). I wish to "maintain" the class in the original project, ProjectA, and simply "use" it in Project B (if that makes sense?!)

From what I have read here and elsewhere, I first need to set up a reference to ProjectA within Project B - but this is the stage I am struggling with.

When I follow what I have read (eg on How to use a Class from one C# project with another C# project ) I get to the stage where I am in the "Add References" dialog, and have selected "Projects".

But VS wants me to reference specific "Component" file types (.dll, .tld, .olb, .ocx, .exe) but I can't seem to find a file that refers to "ProjectA". I browse to the folder where ProjectA sits, but no files of the correct type exist. There are files such as:

ProjectA.csproj, ProjectA.sln, ProjectA.suo

but none of these can be selected in the dialog.

Can anyone provide what I suspect will be a very simple answer? Thanks!

Community
  • 1
  • 1
Jaspos
  • 365
  • 1
  • 4
  • 17
  • 4
    It seems like you're in the `.NET` tab when you try to add a reference instead of the `Project` tab - the project tab should allow you to choose a project _in the same solution_. You can't add a project reference unless the project is in the same solution (and one project can be in multiple solutions). – D Stanley May 19 '16 at 16:16
  • 1
    You'll have to pick the DLL in Project A's bin\Release directory. Some odds that you did not build the Release build yet. – Hans Passant May 19 '16 at 16:21
  • For Hans Passant. If I build a release build (correct I have not done that yet!) this will give me DLLs I can reference? And if so, if I edit the original solution, I presumably need a new release build for those changes to be picked up in my references? – Jaspos May 19 '16 at 16:57

1 Answers1

1

In the dialog box for Add Reference you have the option to reference a project within the solution. This will add all its namespace classes and make them available for your use in the other project. You need to make the two projects part of the same solution to be able to do that.

mymo
  • 239
  • 2
  • 11
  • Ok thanks. To D Stanley & Moataz El Gamal - firstly, how do I add a project to the solution? And secondly, if I add a project to a solution, what happens when I edit the project. So, for example, I add "ProjectA" (from SolutionA) to "SolutionB". If I then edit ProjectA in SolutionB, are those changes reflected in the original SolutionA as well (or does adding a project effectively create a copy of it) – Jaspos May 19 '16 at 16:56
  • To add a project, you right click the solution and click "Add exiting project" or "Add New Project". The solution is visible in "Solution Explorer" The solution is a container that groups related projects. if you plan to reference the classes of a project outside its solution, you can use the DLL or EXE of the project which you can get from its BIN folder. But in that case you will add the reference from file. – mymo May 19 '16 at 17:20
  • Great, that's got it for me, thanks, marked as answer – Jaspos May 19 '16 at 17:32