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.)
deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bbaoIT ConsultantCommented:
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.
deleydSoftware EngineerAuthor Commented:
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.
Jacques Bourgeois (James Burger)PresidentCommented:
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.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Gustav BrockCIOCommented:
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.

kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
Then, in the two other projects, just state:

using MyCommonNamespace;
Once you add a reference to the third project, of course.
deleydSoftware EngineerAuthor Commented:
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?
Console Library Test - Main program wants to call LibraryTest in ClassLibrary1
Jacques Bourgeois (James Burger)PresidentCommented:
In the Solution Explorer, under ConsoleApplicationTest, right click on References and select Add Reference.

Click on the Project tab and activate ClassLibrary1.

Do the same thing under ConsoleApplicationTest2.

Call your method by ClassLibrary1.LibraryTest and it should work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.