Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Sharing Source Files Among Projects

, 1 Mar 2010
Rate this:
Please Sign up or sign in to vote.
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.

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";
#else
            txtMyMessage.Text = "Hello from App2";
#endif

In the above code, only the C# code in the #else, block will be compiled. The code in the #if block will be ignore. 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.

License

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

Share

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 CodeProject.com 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.
 
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 PinmemberMember 40534241-Sep-10 9:05 
GeneralMy vote of 2 PinmemberHomncruse10-Mar-10 7:57 
GeneralRe: My vote of 2 Pinmemberkornman0017-Mar-10 1:21 
GeneralRe: My vote of 2 PinmemberJoel Ivory Johnson17-Mar-10 4:55 
GeneralMy vote of 2 Pinmemberstaffan_v8-Mar-10 21:34 
GeneralRe: My vote of 2 Pinmemberkornman0017-Mar-10 1:10 

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

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

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