How do I break c# into different files?

Started by
3 comments, last by Agwan 15 years, 6 months ago
I have been programming in C++ for years, but I have decided to try c#. So far my entire game is in one increasingly long file. Its getting to be a pain finding certain functions. So my question... Is there a way to seperate a c# project into different files. Maybe put my text input, and button funcions in one file, my mouse functions in another, and so on. Something similar to how you can in c++ Thanks
Advertisement
I have no experience with c# personally, but I do have experience with java, which is supposed to be similar. Surely c# is OO, so you shouldn't have any functions at all, only class methods? In which case, you would for example have a KeyboardListener class which handles keyboard input, a MouseListener which handles mouse input and so on. If you are using VC#, which is I guess pretty much the only likely situation, there is probably a project->add class option somewhere in the menus.

Sorry I can't be of more help, but hopefully someone else can

Mathmo
You have a few different options:

1. Use the same namespace ("Game" for instance) in each file. This way, any public class that is defined in the "Game" namespace is visible to the other classes in the namespace, regardless of what file they are in. Probably the best option for you.

Note that if your main function is defined in a different namespace ("Main" for example), you will have to add a using statement at the beginning of that file to reference the classes in the "Game" namespace. In this case it would be: "using Game;".


2. Use a different namespace in each file, related to the purpose of the classes defined there. The downside is that you would have to add a using statement for each namespace.


3. Declare your class as "partial". Then you can have different parts of a class defined in different files. You probably don't want to have everything in one huge class though. It's best to split it up into multiple classes, each dealing with a separate component of your game.
Strength without justice is no vice; justice without strength is no virtue.
Yeah, my bad, I mean classes.

Thanks so much to all for the help, it gives me some options to play with. Thanks again.
Like Mathmo says, C# is an object orientated language and if I had to guess you're style is more procedural.

The best option for you to take is to break up your program into a series of classes which may represent game objects, input devices or controllers.

Each file in C# generally maps to a class and static classes can provide functions that can be used like functions is a C++ namespace.

Alternatively if you need the file to be that big you can break it up my specifying the class as a partial class.

This topic is closed to new replies.

Advertisement