By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,510 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,510 IT Pros & Developers. It's quick & easy.

Sharing classes (.cs) between multiple projects

P: n/a
Hi !

What is the common use of sharing a single .cs across multiple project
files ?

I think it's to share common classes between projects.
I have actually a .cs file shared accross multiple projects in a same
solution. It's nice to have a common "set" of Classes.

Is this the only way to share classes between projects ?

Are there any side effects to do this ?
I have also found a bug within the ClassViewer :
If I have 2 project and i'm opening the classviewer to view the class
headers from the common .cs file, when I change a class header (or
method) from the common.cs file, only one project reflects in realtime
the changes made in the source code.
Did you experience the same problem ?
Regards,
Cybertof.
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
cybertof <cy****************@gmx.net> wrote:
What is the common use of sharing a single .cs across multiple project
files ?

I think it's to share common classes between projects.


No - it's much better to put everything you want to share into a class
library project which is referenced by other projects. That way you
only get the type defined once, so you won't run into nasty problems
with TypeCastExceptions, you use less memory, you don't run into issues
of the different types being initialized twice, etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
Does a class-library needs to be compiled to be used by other projects ?

In fact, I don't understand what is the difference between a "class-
library" as you name it, and a shared .cs file containing classes
definition to be shared.

When you say "use less memory", what do you mean ?
If i don't instantiate a class from my .cs shared file, i don't use
other not-usefull classes either.

But I'm ready to use class-libraries if it's the good way to do it.
I would like to understrand a little bit more about this.

In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...
cybertof <cy****************@gmx.net> wrote:
What is the common use of sharing a single .cs across multiple project
files ?

I think it's to share common classes between projects.


No - it's much better to put everything you want to share into a class
library project which is referenced by other projects. That way you
only get the type defined once, so you won't run into nasty problems
with TypeCastExceptions, you use less memory, you don't run into issues
of the different types being initialized twice, etc.

Nov 16 '05 #3

P: n/a
cybertof <cy****************@gmx.net> wrote:
Does a class-library needs to be compiled to be used by other projects ?
Yes.
In fact, I don't understand what is the difference between a "class-
library" as you name it, and a shared .cs file containing classes
definition to be shared.
The difference is that a class library will only end up with the type
in one assembly (the library), and be *used* by other projects.
When you say "use less memory", what do you mean ?
If i don't instantiate a class from my .cs shared file, i don't use
other not-usefull classes either.
I'm not talking about instantiation - I'm talking about the memory for
the type itself, and any static variables etc.
But I'm ready to use class-libraries if it's the good way to do it.
I would like to understrand a little bit more about this.


Have you never used a class library before?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
No...
In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...

Have you never used a class library before?

Nov 16 '05 #5

P: n/a
cybertof <cy****************@gmx.net> wrote:
No...


Well, thinking about it, you have *used* a class library, for certain -
you do every time you use the .NET framework at all. It's just you
haven't created your own one :)

In the solution, create a new project of type "Class Library". Add
whatever shared classes you want there.

Then in the other projects, add a reference to that project - bingo,
you'll be able to use the shared classes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.