locked
How do I share a class between projects RRS feed

  • Question

  • This is probably a really silly question, but I'm fairly new to C# and .NET. I have a solution with two projects in it, both windows applications. I have a class that I want to be available to both projects, but I can't work out how to do it. I can't reference one project from the other because they are .EXEs not .DLLs, and I can't add the class to both projects because that creates two separate source files, which obviously defeats the purpose. I'm sure it's quite simple, but I just can't see it.
    Thursday, September 22, 2005 6:24 PM

Answers

  • Yes it is simple...  Take the class you want to share and put it in a newly created Class Library project.  Then reference the Class Library project from your other two projects.

    Class library projects are used to create DLLs.

    And oh... remove the class from your other Application projects.  Smile
    Thursday, September 22, 2005 6:49 PM

All replies

  • Yes it is simple...  Take the class you want to share and put it in a newly created Class Library project.  Then reference the Class Library project from your other two projects.

    Class library projects are used to create DLLs.

    And oh... remove the class from your other Application projects.  Smile
    Thursday, September 22, 2005 6:49 PM
  • Yes I could do that, but then I'm creating a whole new DLL just for this class which seems a bit inefficient. Surely it must be possible to arrange for one source code file to be compiled into two projects. It was easy enough in VS6.
    Friday, September 23, 2005 7:44 AM
  • Well there is also the Link File option.

    Create your class in Project1.  In Project2 right click on the Project and select Add|Add Existing Item.  Browse to the class file then click on the Open button's down arrow and select Link File.

    See if that is what you want.

    I've never really used this feature and I'm not sure of the ramifications or difficulties it causes such as with source code control, etc.  This is however one technique used for creating global AssemblyInfo.cs files to share across framework libraries.
    Friday, September 23, 2005 12:29 PM
  • That's what I was after. Brilliant. Thanks
    Friday, September 23, 2005 3:01 PM
  • even i was trying to do the same thing. share a vb class between two projects. but when i followed the above steps and selected the class and clicked on down arrow button next to OPEN. all i see are the options Open and Open With but no link to. can somebody help.
    Wednesday, January 11, 2006 4:29 PM