Forum C# Multiple cs files

Multiple cs files

Postby WindieGames » February 28th, 2011, 12:25 am

Hello,

I apologize but I'm new to the c# environment, and even though the syntax is similar to other languages I've worked with, I have a couple questions.

1) Am I able to use multiple cs files? (I find the one cs file is getting extremely cramped and hard to find methods, even when collapsed.)

2) If so, how would I go about doing that? What kinds of statements do I need to use to import another cs file, or can I just add a cs file to the project and call functions from that?

Thanks,
Chris
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: Multiple cs files

Postby Henrik » February 28th, 2011, 1:00 am

Right-click the project -> Add -> New Item... -> Class, and then name your new class whatever you want. It should go in the same namespace as your existing classes, which means you don't need to do anything special to reference it.

Normally in Visual Studio projects you have one file per class, smaller container classes or enums can go in the nearest related class file, and if you have really large classes, you can even split up a class over multiple files, but you have to use the partial keyword for that.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Multiple cs files

Postby WindieGames » February 28th, 2011, 1:22 am

Ok, I was successful with most of what you told me to do. I guess that part I'm trying to figure out right now is how to reference the methods in the other class file. It's in the same namespace, but the methods are in a different class. So do I have to create an instance of a class in the other cs file and call its methods through that? Or can I just call a method without an instance?

I hope I'm explaining myself clear enough.
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: Multiple cs files

Postby WindieGames » February 28th, 2011, 1:34 am

Nevermind, I answered my own question. Thanks for your help.
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: Multiple cs files

Postby Henrik » February 28th, 2011, 2:12 am

Hm, we should probably have a little object-oriented tutorial somewhere for times like this. :-)

A class has fields (variables) and methods (functions). Those can then be static or non-static. Static methods and fields are "global", they can be accessed directly without having to make an instance of the class.

In the code you already have you should have non-static fields and methods on your Game and Player classes, and then the server takes care of creating instances of those as needed.

I guess that what you did now was to create an instance of your new class in your Game class somewhere, and then call methods on it? If your new class holds functionality that isn't really tied to an instance of Game, if you have methods that only return something based on the arguments, for example calculating coordinates or whatever, then you can make those methods static, and then you don't have to create an instance of your new class.

Or whatever, do what makes most sense to you. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm


Return to C#



cron