Sharing classes (.cs) between multiple projects


C

cybertof

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.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

cybertof said:
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.
 
C

cybertof

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.
 
J

Jon Skeet [C# MVP]

cybertof said:
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?
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

cybertof said:
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.
 
Ad

Advertisements


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