locked
Sharing source code files among different projects / solutions RRS feed

  • Question

  • I have a situation where 2 projects need to use functions which are very similar - i.e. they share about 95% of the code but have some minor differences.  I was hoping I could include the same C# source files in multiple projects and use the #if - #endif conditionals to control compiling the code in the areas that differ.  Unfortunately there does not appear to be any easy way to do that in VS 2008.  If I create a new project file and then use the add existing item option to try to add code from a different directory, VS 2008 copies the original file into the new project's directory instead of just linking to the original file.  Since my intention is to have 1 set of common source files shared, this is not what I want.  I also cannot compile those as a separate project and include the project in multiple solutions since that would mean the #if - #endif stuff would not work correctly.  Is there any way I can link the same C# source file to multiple projects, compile each project with its own set of #if directives and have multiple .dll files created for my different solutions?
    Friday, October 17, 2008 4:36 PM

Answers

  • Well,

    Here is one solution.

    Put both your projects in the same directory and when you create your projects through VS, make sure you choose option TO NOT create directory for the project. So, You .csproj files and source files will all be in one directory and I think then you'll be OK.

    Let me know if this helps.

    Viral.
    Saturday, October 18, 2008 5:23 AM
  • I can see you never programmed in C/C++...The use if #ifdef directives used to be used a lot. Frankly I haven't used one in about eight years. Reason....maintenance. Wait to you have to debug, its a pain in the but. It may be easier in the short term but it incurs a maintenance overhead if things change, either for the client or the main code. IMHO

    William Wegerson (www.OmegaCoder.Com)
    Friday, October 17, 2008 10:45 PM

All replies

  • Think of this in OO terms and not files. Create a library assembly which contains the full common code used by both consuming projects. The differences which you mentioned should be expressed in the target consumer project by creating a class derived on the common class, but its methods overrides the base and the functionality which is different is used for that project only.

    Or along the same lines in the base class make the operations abstract and require it to be overrided by a consumer...therefore its guaranteed to be properly generated by the consumer, otherwise it can't be used...type safety.

    Hence you have the commonality needed, but the expression of the differences are localized in the consumers.


    William Wegerson (www.OmegaCoder.Com)
    • Edited by OmegaMan Friday, October 17, 2008 4:53 PM Removed two hences
    Friday, October 17, 2008 4:46 PM
  • What OmegaMan says is considered "best practice". However, there are some times when you really need to just link files across projects.

    It's very simple: From the Solution Explorer, right-click and select "Add Existing Item...". Now browse for the file(s) you want to include and select them.  Instead of clicking OPEN, click the little drop down on the right-hand side of the OPEN button and select "Create Link" (can't recall exact verbage right now, but you'll get it).

    This creates a link to a source file (or files), rather than making a copy and inserting it into your project.

    *edit - The button on the Add Existing Item form is labeled "Add", and the drop down item you want is labeled "Add As Link".
    • Edited by Tergiver Friday, October 17, 2008 5:26 PM
    • Proposed as answer by programming girl Monday, August 6, 2012 7:12 PM
    Friday, October 17, 2008 5:24 PM
  • I've never shared files between project (fun to know it's possible).

    What I do: I put all the functionality that are common on both applications into it's own dll/project. Who ever needs access to these functionality just add the project/dll into the solution.

    Think of it as creating your own 3rd party library.
    Puzzles, brain teases, riddles, enigmas: http://www.toysforthebrain.com
    Friday, October 17, 2008 8:03 PM
  • This is one of those situations where I believe I really do need to share code.  I have a companion product that is using some of the features of the original product, but the fields on the form would be read only.  It just seems simpler to include the same code files in both projects and use #if for the minor differences and also to trigger the read only on some properties.  The forms displayed are built dynamically depending upon database contents and which product is accessing the info.  Thanks for all the help and the prompt responses!
    Friday, October 17, 2008 8:25 PM
  • I can see you never programmed in C/C++...The use if #ifdef directives used to be used a lot. Frankly I haven't used one in about eight years. Reason....maintenance. Wait to you have to debug, its a pain in the but. It may be easier in the short term but it incurs a maintenance overhead if things change, either for the client or the main code. IMHO

    William Wegerson (www.OmegaCoder.Com)
    Friday, October 17, 2008 10:45 PM
  • Well,

    Here is one solution.

    Put both your projects in the same directory and when you create your projects through VS, make sure you choose option TO NOT create directory for the project. So, You .csproj files and source files will all be in one directory and I think then you'll be OK.

    Let me know if this helps.

    Viral.
    Saturday, October 18, 2008 5:23 AM