Click here to Skip to main content
13,248,087 members (37,132 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 1 Mar 2010

Sharing Source Files Among Projects

, 1 Mar 2010
Rate this:
Please Sign up or sign in to vote.
How to share source files among projects

There are times when you will want to share the same source code among several projects. A common way to do so is with a shared assembly; you put common functionality in one project and then share the output among several other projects. But at times, this solution isn't suitable such as when you have functionality that you plan to share across more than one .NET runtime (example: Desktop Framework, Compact Framework, and Silverlight Runtime). For these cases, you can copy your source code to the projects for all three run times. But then you end up with three branches of code and may need to make sure they are synced up with each other.

It is possible to use the same source file in different projects by adding a link for the file from another project so that each project is using the same runtime. Since it is the same physical file, changes to the file done from one project are visible to all the projects using the same linked file. Adding a linked file is easy. To link to one file's project from another, right-click on the project, select Add->Existing Item and navigate to the file. Once you've found the file, click on it and then click on the down triangle on the Add button and select "Add as Link."

A potential problem from using this solution is you may have items in a class that you don't want to be visible in another class. You can selectively hide sections of code using a few preprocessor directives. As a simple example, let's say I made a Windows Form application and I have all of the files from it linked to a second project. I have code in a method that is setting the text on a label. But I want the text to be set differently depending on the project in which it is run. The preprocessor directives I will use are #if, #else, and #endif.

#if App1
            txtMyMessage.Text="Hello from App1";
            txtMyMessage.Text = "Hello from App2";

In the above code, only the C# code in the #else block will be compiled. The code in the #if block will be ignored. For my first project, I want to code in the #if block to be used. To accomplish this, I need to add a Conditional Compilation Symbol. I right-click on the project and select Properties. Under the Build tab, I can add conditional compilation symbols. I've done this for the first project and have added a symbol named App1. So now the first block of code will get compiled and the second block ignored.

While this solution has its advantages, it is not the end-all solution for sharing functionality across projects. If you find yourself using excessive conditional compilation blocks in your code, then you may have reached a point at which it is better off having two separate source files.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.

For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.

While most of my articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.


You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Member 40534241-Sep-10 10:05
memberMember 40534241-Sep-10 10:05 
Usefull information, would have gotten 5 if it had also included info on using source control to achieve the same ends.
GeneralMy vote of 2 Pin
Homncruse10-Mar-10 8:57
memberHomncruse10-Mar-10 8:57 
GeneralRe: My vote of 2 Pin
kornman0017-Mar-10 2:21
memberkornman0017-Mar-10 2:21 
GeneralRe: My vote of 2 Pin
Joel Ivory Johnson17-Mar-10 5:55
memberJoel Ivory Johnson17-Mar-10 5:55 
GeneralMy vote of 2 Pin
staffan_v8-Mar-10 22:34
memberstaffan_v8-Mar-10 22:34 
GeneralRe: My vote of 2 Pin
kornman0017-Mar-10 2:10
memberkornman0017-Mar-10 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 1 Mar 2010
Article Copyright 2010 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid