Is there a standard file to place classes?

  • Thread starter Thread starter Steve1 via DotNetMonster.com
  • Start date Start date
S

Steve1 via DotNetMonster.com

Hi all,

I'm new to C# and could do with alittle help.
I'm currently producing a data conversion app. So far this app only has a
Win form which is the user interface. I am finding this file growing large
very quickly due to all the classes I am creating. I would to know if C# has
a particular file that is meant to store these numerous classes and that
allows me to call these classes (and pass arguments) when needed??

Any help would be very much appreciated.

Many thanks,
Steve.
 
Hi.
1.How about splitting the class into 2 or more classes?
Create a separated class that performs the logic.

2.C# 2.0 support partial class!
If you use .NET 2.0 beta 2, you can save a single class into 2 or more
files.
 
The C# standard is to place each class in its own file, where the file
name is the same as the class name.

Of course, there are exceptions: I have several files that contain
multiple classes. However, these are the exception, not the rule.
 
Hi cat,

Thanks for your reply. I'm using VS.Net 2003. I don't mind splitting my
classes up but I would like to know if there is a standard file/files that
are meant to store the numerous classes that an app soon comprises. At the
moment my Win form is becoming larger and larger due to all the classes
created. Is there a file that is meant to store classes? What is a class
library (the file you can choose thats on the same screen as Win forms, Web
forms, Control libraries etc...)

Many thanks again,
Steve.
 
Hi Bruce,

Thank you for replying. Firstly, what are these files called? Is there a
specific name for these types of files? Is there any links that you know of
or can you possibly give a brief example of how to create one of these files?
Secondly, if each class is meant to have it's own file it wouldn't be unusual
for a project/app to hundreds or even thousands of files??

Many thanks again,
Steve.
 
Steve1 said:
Hi Bruce,

Thank you for replying. Firstly, what are these files called? Is there a
specific name for these types of files? Is there any links that you know of
or can you possibly give a brief example of how to create one of these files?
Secondly, if each class is meant to have it's own file it wouldn't be unusual
for a project/app to hundreds or even thousands of files??

Many thanks again,
Steve.

Just right click on the project and select Add Class. Or you can just
add an empty code file (.cs) to your project. Call it whatever you
like. Then copy and paste your class(es) into the file.
 
When you get up into the hundreds of classes, you might think about
dividing your project into libraries (DLLs). However, that's a ways
down the road, I think. For now, just follow Chris's instructions: open
up the Solution Explorer, right click on your project (not the solution
name on the first line... the project name below it); choose "Add
New...", choose "Class"; give the file the name of one of your classes;
erase the part starting with "public class..." and then cut/paste the
class from your existing form .cs file into the new file. Your project
should compile just fine.

Rinse and repeat, until your classes all have their own files. :)
 
Hi Chris and Bruce

Thanks both for replying. Is there any particular way to reference the
classes in the seperate class file?
 
Steve1 via DotNetMonster.com said:
Thanks both for replying. Is there any particular way to reference the
classes in the seperate class file?

Just as you would any other class - use a "using" directive at the top
of the class if it's in a different namespace, but if it's in the same
namespace you don't need to do anything at all.

If you use a class from a different project, you need to add a
reference to the other project in the project you wish to use it from,
but you don't need that if the classes are all in one project.
 
Back
Top