Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

Visual Studio: Moving common code to a separate project

I created a Visual Studio C# solution with one project.

Then I created another similar Visual Studio C# solution with one project.

1. Now since the projects are similar I would like to combine the two projects into one solution so I have two projects in one solution.

2. The two projects have a lot of duplicate code, so I'd like to create a 3rd project in the same solution and move all the duplicate code to the 3rd project, and then have projects #1 & #2 reference that common code, somehow.

Those are my two questions: How do I merge two solutions into one solution with two projects; and then if I create a 3rd project and put common code in that 3rd project, how do I access that common code from projects #1 and #2?

I want the result to be two different executables, which is why I have 2 projects; but of course it would be nice not to have all that duplicate code in both projects.

(I have Visual Studio 2010 at home and 2013 at work.)
Avatar of bbao
bbao
Flag of Australia image

the recommended way is to split the common codes from the projects and put them somewhere (local or network share) accessible to all existing and new projects, treat them as a codes library, then INCLUDE the head and implementation files the projects, individually.
Avatar of deleyd

ASKER

Thank you. This common code will only be common to this solution.

I'm looking for the steps how code in one project can call code in another project when both projects are in the same solution.
Do you have good reasons to keep all of this into separate projects? Usually, you have many projects in a solution when some of these projects need to be used in other solutions.

An example of that would be a solution where you have the main application, using another project that deals with database access and that you would want to use in one or more other solutions that also need to work with the same database. You might also have some project that contains features that you want to use in all your solutions, such as constants for e-mail addresses and the company logo, login or bug report forms, common methods to calculate taxes, etc.

Otherwise, if all these projects are used only in that solution, it makes more sense to combine them into one single project.
As I understand it, all you need to do is to move the common code to a new third project within the same solution.
Then, in the two other projects, just state:

using MyCommonNamespace;

But if all you really wish is not to have the code files mixed up, you can create a folder and drag-n-drop the files with the common code to this folder.

/gustav
Then, in the two other projects, just state:

using MyCommonNamespace;
Once you add a reference to the third project, of course.
Avatar of deleyd

ASKER

I almost got it.
Here's a screen shot of my test.
Creating two executable programs:

1. ConsoleApplicationTest
2. ConsoleApplicationTest2

Both want to call LibraryTest in ClassLibrary1.

How would I code that call?
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial