Tech Off Thread

8 posts

Code re-use in Visual C# .NET Projects

Back to Forum: Tech Off
  • User profile image
    asqui

    I am trying to re-use a generic database selection form between two projects by pulling the *.cs and *.resx files into another directory and then having both projects use those files.

    Unfortunately, having done this, I can't work out how to include this form in both my projects.

    If I "Add > Existing Item..." to my project it simply copies the files into my project directory.

    I tried hacking the *.csproj file as follows:

    Before:
    <File
        RelPath = "ServerSelectForm.cs"
        SubType = "Form"
        BuildAction = "Compile"
    />
    <File
        RelPath = "ServerSelectForm.resx"
        DependentUpon = "ServerSelectForm.cs"
        BuildAction = "EmbeddedResource"
    />



    After:
    <File
        RelPath = "..\_Common\DatabaseSelectorForm\DatabaseSelectorForm.cs"
        SubType = "Form"
        BuildAction = "Compile"
    />
    <File
        RelPath = "..\_Common\DatabaseSelectorForm\DatabaseSelectorForm.resx"
        DependentUpon = "..\_Common\DatabaseSelectorForm\DatabaseSelectorForm.cs"
        BuildAction = "EmbeddedResource"
    />


    If I do that Visual Studio doesn't like that -- it ignores those entries, doesn't load the form, and, upon saving the project, simply strips them out of the new project file!


    I strongly suspect that my whole approach to code re-use is a bit "vb6" -- could someone enlighten me to the 'proper' way to do this?

    (I was trying to avoid going all-out and compiling the form into a standalone DLL as I think that would be overkill.)


    Thanks in advance.

  • User profile image
    footballism

    Create new Items with the same names respectively in your current project, and copy all the codes from the original files to the newly created corresponding files.

  • User profile image
    Yggdrasil

    There are two approaches here.
    The object-oriented approach is to create the form just once, in one project, and reference it from the other two projects. This way you just instantiate it twice. You describe it as overkill, but I don't think it's that horrible. Certainly neater, since you won't have the same class defined twice in different place in the binary.

    The second approach, if you just want to share the same source file for two projects, is to link it to two projects. When you do Add Existing Item, note the little dropdown arrows on the Open button - this allows you to do a Link instead of creating a new copy of the source file.

    Do note that this has a tendency of messing with VSS and other source-control systems, but nothing that can't be managed.

  • User profile image
    TommyCarlier

    A good way to do it: split your functionality in multiple layered projects. At work, my solutions have these projects:
    - Common: common functionality, used everywhere (client, server, ...)
    - Client: client functionality, common for all applications we write
    - Server: server functionality, common for all applications we write
    - XClient (replace X with app.name): client functionality, specifically for 1 application
    - XServer (replace X with app.name): server functionality, specifically for 1 application

    So application A will have these DLLs: Common.dll, Client.dll, Server.dll, AClient.dll, AServer.dll
    And application B will have these DLLs: Common.dll, Client.dll, Server.dll, BClient.dll, BServer.dll

    Your generic form would be placed in the Client-project, so all your applications can use it (and subclass it to add functionality).

  • User profile image
    kojiishi

    You don't have to "hack".

    In the "Add existing item" dialog, click the dropdown menu at the right of the "Add" button. You will see "Link" there.

    The "Link" feature is what you need.

    I use this often. In my project, I have 2 DLLs, but they are separated just becase whether it is shared by other proejcts or not.

    So to keep logical separation in source code tree, I create classes to where it should belong to from architectually point of view.

    And then if the class needs to be accessed from other DLLs, I use "link".

  • User profile image
    asqui

    Thanks guys.
    I completely missed the "Link" option in the Open button's drop-down.

    Works a treat!

  • User profile image
    W3bbo

    kojiishi wrote:
    You don't have to "hack".

    In the "Add existing item" dialog, click the dropdown menu at the right of the "Add" button. You will see "Link" there.

    The "Link" feature is what you need.


    Is this (yet another) feature missing from VS2003 Academic?

    All I get is "Open" and "Open with..."

  • User profile image
    footballism

    W3bbo wrote:

    Is this (yet another) feature missing from VS2003 Academic?

    All I get is "Open" and "Open with..."

    Dude, if my memory is still reliable, this feature does exists in VS2003:)

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.