Sign in to follow this  
SelethD

How do I break c# into different files?

Recommended Posts

SelethD    456
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

Share this post


Link to post
Share on other sites
Mathmo    163
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Agwan    122
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this