how to use a common class in different projects

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

newb starting to learn dotnet (vbnet 2008 express - and c#express)
I have a few projects i'm working on
I have a utility class that i thought i'd use in different projects, for
example to log debug notes to file to track runtime status and other general
utility functions.
so i moved it from the project folder into it's own folder "common"
(naturally since i moved it the project cant' find it anymore...which is
what i expected)
so i put it back into the project by draging from explorer into the vbnet
ide
I thought it would then reference that file location from the "common"
folder (and other projects could do likewise - so i only have one copy to
maintain)
However, I notice the file was copied by the system (vb ide?) back into the
project folder...
i was trying to get away from having multiple copies of the same file and
having to remember to update it in each project folder if i make changes to
it in other projects as i'm working and learning and creating additional
functions this utility class could offer.

how does one handle this in dotnet?
thanks
mar
 
newb starting to learn dotnet (vbnet 2008 express - and c#express)
I have a few projects i'm working on
I have a utility class that i thought i'd use in different projects, for
example to log debug notes to file to track runtime status and other general
utility functions.
so i moved it from the project folder into it's own folder "common"
(naturally since i moved it the project cant' find it anymore...which is
what i expected)
so i put it back into the project by draging from explorer into the vbnet
ide
I thought it would then reference that file location from the "common"
folder (and other projects could do likewise - so i only have one copy to
maintain)
However, I notice the file was copied by the system (vb ide?) back into the
project folder...
i was trying to get away from having multiple copies of the same file and
having to remember to update it in each project folder if i make changes to
it in other projects as i'm working and learning and creating additional
functions this utility class could offer.

how does one handle this in dotnet?
thanks
mar

You have at least a couple of ways to do this.

The first way, if you actually want your class in the project you are
working at the time, is to add the class file as an existing item. To
do this you go to the "Project" menu, and select "Add Existing Item".
I'm using VS 2010 RC for this, so the VB Express 2008 may be slightly
different. When the dialog comes up to navigate to your file, notice
the "Add" button has a small drop down arrow on the right. After you
select your file, select the drop down arrow and select "Add as Link".
This adds the file to your project, but leaves it in the common folder.

The second way is the way that I prefer handling this situation. Use
the code in your Common folder to create a class library project.
Create a library in your folder that you add as a reference to your new
projects. You need to browse to the folder containing the dll rather
than finding it in the list of .Net dlls on your system. This has the
advantage of keeping you from modifying the source to the common class,
which could possibly break codes in other projects linked to the source
file. A breaking change would be rare in option one, but it could
happen if you aren't careful.
 
oh, i bet if i set copy local to false that will fix it?
no, that didn't work.
maybe i have to compile that class alone into a dll so i can add as a
reference?
 
To include shared files in a project w/o copying, do this:

Project
Add Existing Item
Browse to the shared directory
Select one or more files
Click the down arrowhead symbol at the right edge of the Add button
Add as Link
 
Thanks Mike and AMercer

AMercer said:
To include shared files in a project w/o copying, do this:

Project
Add Existing Item
Browse to the shared directory
Select one or more files
Click the down arrowhead symbol at the right edge of the Add button
Add as Link
 
The second way is the way that I prefer handling this situation. Use
the code in your Common folder to create a class library project.
Create a library in your folder that you add as a reference to your new
projects. You need to browse to the folder containing the dll rather
than finding it in the list of .Net dlls on your system. This has the
advantage of keeping you from modifying the source to the common class,
which could possibly break codes in other projects linked to the source
file. A breaking change would be rare in option one, but it could
happen if you aren't careful.

I also like to keep common code in a class library project - but, I prefer to
use project references, specifically so that I can modify the code. Many
times, I find I want to add new methods, classes, interfaces, etc or even fix
the occasional bug- and I find it to be very convenient to be able to handle
this without opening a completely different project. Yes, there is the risk
of breaking changes - but, as long as you dont' change interfaces, etc it's
not much of an issue. Besides that, I have all my stuff in continous
integration, so if I do make a change, all of the projects that are dependant
on that library are immediately rebuilt and their unit tests run. So, I know
if I've broken something rather quickely :)
 
You would not create classes in a project, to use in other projects,

Add to your project a new Library Project and put the class in that.

Everything for that project is created in its own folders.
 
Back
Top