9

I'm kinda new to c# and I'm trying to figure out a good solution for keeping common code somewhere that is used by several projects.

First I tried keeping it in a .cs file and add it to the projects that needs the code, but this COPIES the file meaning if I do any changes to the common code it will not be reflected in the various projects using this code.

Then I tried creating a class library instead and reference the DLL within the projects using the code. First of all I can't get this to work even though I am following the tutorials, Even when I reference the DLL I still cannot use it somehow, but even if this worked it still needs an extra DLL to be bundled with my app for some common code and that is not what I want.

Is there any way to keep common code that is REFERENCED without needing to ship a DLL w

5 Answers5

12

The best solution is to have a common library that you reference in each of your projects. However you say you don't want to do that as it requires bundling additional dlls, is that really that big a issue? You also mention that you can't get that to work, post the details of errors etc and we will be able to help.

Another option, assuming you are using Visual Studio is to link the common file into your project rather than add it. This will keep the common file in its current location and not move it into your project directory.

To do this, go to the add existing file dialog but on the 'Add' button click the down arrow and select 'Link' instead.

As mentioned above using a common library is the best solution so if bundling this is a unsurmountable issue you could look into using ilmerge. This will merge all your dlls/exes into a single file that you can then deploy.

MrEyes
  • 13,059
  • 10
  • 48
  • 68
  • Great, I've never noticed that little arrow to add as a link instead, that was exactly what I needed, thanks! – Kristian Erik Tigersjäl Dec 03 '10 at 13:22
  • As to why adding a class library fails I have no idea, I've created the class library and compiled it and it looks fine. I reference it in the solution using it but when I try adding the using code for it or even try to access it it's as if it's not been referenced at all, and I cannot see anything wrong with the library code either strangely enough. – Kristian Erik Tigersjäl Dec 03 '10 at 13:23
  • Removed actual code, but this is what it looks like: using System; namespace textfile { class textfiletools { public static bool readfile(string filename, out string[] dest) { } public static bool writefile(string filename, string[] data) { } } } – Kristian Erik Tigersjäl Dec 03 '10 at 13:24
  • Its difficult to be certain but this sounds like either a namespace issue or maybe all the classes in your common library are marked as private or internal. EDIT: After seeing your last comment, if you make the textfiletools class public you should be able to use it (i.e. public class textfiletools). Without an access modifier the class is assumed to be private so your referencing library won't be able to see that class or the methods within it, even though these are marked a public. – MrEyes Dec 03 '10 at 13:24
  • all classes marked as public apart from the "parent class" but I tried marking that public too with no effect. this one really puzzles me I'm sure it's just a stupid mistake I cannot see which makes it bother me even more. ;) EDIT: NOT parent class, I meant the methods are all public but the class is not and changing it didn't make any difference. – Kristian Erik Tigersjäl Dec 03 '10 at 13:28
  • 1
    You need a solution with two projects. Under Tools -> Options -> Projects and Solutions make sure "Always Show Solution" is checked. Now open the solution as you normally would and right click to add existing project. Add the class library that you want to share between projects. Then add a reference from the main project to the class library project (project reference not DLL reference). Don't forget that C# uses namespaces automatically so you will need to make sure you have the correct "Using xxx.xxx.xxx" in your source file before things will work. – Ryan Pedersen Dec 03 '10 at 13:30
  • Ryan: now THAT I haven't tried, I'll give it a shot, had no idea I had to add the project like that. – Kristian Erik Tigersjäl Dec 04 '10 at 10:46
  • FIXED, after alot of checking I realized that I had forgotten making the class public which makes the namespace invisible. Apparently when calling the class from the program I am not considered to be in the same scope as compared to using a class file imported into the project. – Kristian Erik Tigersjäl Dec 05 '10 at 10:51
  • 1+ i know this is old post but i have a question please, best solution is "common library" but m facing a issue when common library using in web service project and UI project it works, but when add COM referenced in Web-Service and UI-Project and add WS reference in UI then get conflict and i can see two references with same COM.dll with difference names in that case what should i do how can handle this case? however i can remove additional reference it work but again come when refresh WS. – Haseeb Jan 13 '19 at 11:33
3

You can add a link to a common file:

  1. Right click on the project
  2. Select 'Add' => 'Existing Item...'
  3. Find the .cs file you want to share
  4. Click the drop down on the Add box
  5. Choose, Add as Link.
halfer
  • 19,824
  • 17
  • 99
  • 186
Nick
  • 25,026
  • 7
  • 51
  • 83
1

You were almost there. You can add your existing file, but note that you can expand the "add" button and choose to link to it instead.

Jon B
  • 51,025
  • 31
  • 133
  • 161
0

You can add a linked file to a project - this does not create a copy but simply references it.

See the "Adding an Existing Item as a Link" section in the linked page:

To create a link to an existing item:

  1. In Solution Explorer, select the target project.
  2. On the Project menu, select Add Existing Item.
  3. In the Add Existing Item dialog box, locate and select the project item you want to link.
  4. From the Open button drop-down list, select Add As Link.
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

In visual studio you can add a link to an existing item:

In the solution explorer, right click and choose Add -> Existing Item... Browse to the file and instead of clicking Add, choose Add As Link.

But I would advise to use a separate project for common code.

Samuel
  • 1,374
  • 10
  • 16