What is the best way to use one class in more than one project?

L

Lisa B.

I'm relatively new to vb.net and visual studio 2005.

I have over a dozen projects created, and several of them share classes.

I've been using the "Project...Add Existing Item..." menu item to reuse my
classes, but this introduces a very big problem -- the problem is that when
I add the class to my project, it *copies* the vb file to the current
project...thus, each of the projects that use a given class have their own
copies of the class definition, and so if I improve, fix, or add to my
class, it only effects whichever project was active when I did the
modification...this is definitely NOT what I would want!

Basically, I would like to use my own custom classes in multiple projects,
but I want all the projects to refer to the same file, so that I can edit
the class from within any project that uses it, and have my revision be seen
by all the projects that use that class. What is the "standard" or
"recommended" way of doing this?

I think this is a very simple and commonplace code management problem, yet I
haven't seen it addressed in any of the basic .NET books I've read so far.

Lisa B.
 
Z

zacks

I'm relatively new to vb.net and visual studio 2005.

I have over a dozen projects created, and several of them share classes.

I've been using the "Project...Add Existing Item..." menu item to reuse my
classes, but this introduces a very big problem -- the problem is that when
I add the class to my project, it *copies* the vb file to the current
project...thus, each of the projects that use a given class have their own
copies of the class definition, and so if I improve, fix, or add to my
class, it only effects whichever project was active when I did the
modification...this is definitely NOT what I would want!

Basically, I would like to use my own custom classes in multiple projects,
but I want all the projects to refer to the same file, so that I can edit
the class from within any project that uses it, and have my revision be seen
by all the projects that use that class. What is the "standard" or
"recommended" way of doing this?

I think this is a very simple and commonplace code management problem, yet I
haven't seen it addressed in any of the basic .NET books I've read so far.

Lisa B.

Put all of your shared classes in a ClassLibrary project, and
reference that DLL with any other project in the solution that needs
to.
 
G

Guest

I'm using vs 2003, but I assume it is similar with 2005. When you do 'add
existing item', the 'Open' button has a dropdown on the right edge. Activate
it and select 'link file'. That will reference the original file rather than
making a copy.

I have a project dedicated to testing my shared files, and that project is
where the shared files actually exist. All other projects link to these
files as needed.

Be advised, there are perils with this approach. You can change things for
the sake of one project and break other projects. But with care, it is
effective. It is especially beneficial for things like api declarations
(declare statements, supporting structures, constants, .net glue functions
that call the api).
 
G

Guest

I'm using vs 2003, but I assume it is similar with 2005. When you do
'add existing item', the 'Open' button has a dropdown on the right
edge. Activate it and select 'link file'. That will reference the
original file rather than making a copy.


Would it be better to create a library?
 
P

Patrice

They is no "best" way. There is 3 ways I can think of :
- first you can share the same source code file into multiple projects
(adding the source file as a "link"), changes are then taken into account
when you recompile a given application
- you can also create a class library and have this library installed along
with the main exec file, you can then deploy changes (or keep thins
unchanged) on a per application basis
- or do the same and put the library in the GAC a central location from
which libraries are shared (most likely once you have general tools that are
firmly grounded and changed unfrequently)
 
G

Guest

Would it be better to create a library?

Yes IMO if the code in question is changed rarely.
 

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