Sharing Classes across projects

G

Guest

I have created a class which I wish to reuse amongst several projects.
But each time I Add the class to a new project, it creates a copy of that
class in the folder of the new project.
This means that when I make changes to that class, I have to apply the same
changes to all the projects.

Is there a setting that I have missed such that I can share one class
amongst several projects?
 
P

Peter van der Goes

Tinius said:
I have created a class which I wish to reuse amongst several projects.
But each time I Add the class to a new project, it creates a copy of that
class in the folder of the new project.
This means that when I make changes to that class, I have to apply the
same
changes to all the projects.

Is there a setting that I have missed such that I can share one class
amongst several projects?

Try putting your class or classes in a C# Class Library project, then create
a reference to the resulting dll in the other projects that use the class.
 
B

Brian Gideon

If you want to share a class between projects I recommend putting that
code in a class library that can then be shared by multiple projects.
If you want to share a file between projects then instead of clicking
open while adding an existing item click the arrow right next to the
open button and choose to link the file instead. I find this most
useful when you have a file that contains assembly attributes that are
common to many projects in a solution. Like I said though, I don't
recommend doing this for a file that contains a class.

Brian
 
B

Bob Powell [MVP]

Compiling the class into a DLL will enable you to reuse it. If you create
references to the DLL in your projects it will be copied to the bin
directory of the project for testing and distribution. If you only want one
copy of the DLL on the machine regardless of how many times you use it
you'll need to give the assembly a strong name and install it in the Global
Assembly Cache (GAC).

See MSDN for how to do strong names.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

Thanks Brian,
Linking the Class is exactly what I was looking for.

The dll solution as mentioned by Peter van der Goes and Bob Powell will
serve me well when the shared classes have stabilised. But while the shared
classes are still in a state of developmental flux, I need to be able change
bits of them on the fly.

Cheers

Tinius
 
L

Leon Lambert

This assumes you are using Visual Studio:
There is no reason to wait till it is stablized. Just put all projects
into a solutions and make all the ones using the shared dll dependant on
it. This way all files are there and they will compile in proper order
if you change anything. This is something I commonly do.

Hope this helps
Leon Lambert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top