Forum C# New to C# and can't figure out how to organize classes

New to C# and can't figure out how to organize classes

Postby macdonald.lethe » October 1st, 2011, 2:24 am

Long time timeline coder here, had to learn classes for both Flash and C# for playerio.

I managed to get a multiplayer game working, where me and my friends controlled the pieces and all worked well.

I had a little bit of trouble working with classes in Flash, but eventually I figured it out because I had static variables and functions, which in a way resembled how I worked with in the Timeline.

However, from what I understand, you can't work with static methods and properties with playerio, so I got compeltely stuck and can't code anything other than the basic setup of my game.

Please bear in mind I'm not a professional programmer, and I have no idea how real class based projects are managed, and that reading books or the internet did not help me at all figure that out.

Let me show where I am stuck:

public class EnemyBalls
{ //enemy properties }

[RoomType("MyCode")]
public class GameCode : Game<Player>
{
//here I started setting up my first game variables
public List<EnemyBalls> enemyBallArray = new List<EnemyBalls> { null };

.... more code

Now, when the server receives the message "newGame", I tried calling a function to create enemyBalls of the type EnemyBalls and store them at the enemyBallArray.
But since I can't call any static functions, and I can't create/instantiate the enemies inside the GameCode class, I'm completely lost.

I can't create a class object to let me call functions, so where do I go from there?

(also, another doubt I had was why I can call -this- or instantiate a new class object from inside a class method, but not outside a method along with the properties? What is the reasoning behind that?)

Hope someone can help me
macdonald.lethe
Paid Member
 
Posts: 14
Joined: September 24th, 2011, 8:42 pm

Re: New to C# and can't figure out how to organize classes

Postby macdonald.lethe » October 1st, 2011, 2:52 pm

I decided to try something out after some thinking and it worked.

I created a new class, here is the example code:

public class StoreVariables
{
public List<EnemyBalls> enemyBallArray = new List<EnemyBalls> { null };

//created a public class where I stored variables, to work with them as if they were static properties
}


[RoomType("MyCode")]
public class GameCode : Game<Player>
{

StoreVariables storedVariables = new StoreVariables ();

...

Somehow I was under the impression I couldn't declare a new class object inside gameCode.

Now I have an object that can access those variables. If I want to work with more classes from inside GameCode, I will probably have to reference storedVariables to them.

While this works, is this a proper way to do it? If anyone can recommend a better way or can confirm this is how I should go about it please let me know
macdonald.lethe
Paid Member
 
Posts: 14
Joined: September 24th, 2011, 8:42 pm

Re: New to C# and can't figure out how to organize classes

Postby fox1980 » October 4th, 2011, 1:11 pm

Instancing objects is the bread and butter of any developer using an OO language, there's nothing wrong with it.
Static classes should only be used when you're sure you don't need more than one instance of an object.
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: New to C# and can't figure out how to organize classes

Postby fox1980 » October 4th, 2011, 2:27 pm

By the way, i just did a test and Static Classes seem to be allowed now. Are you using the latest development package ?
fox1980
 
Posts: 206
Joined: April 1st, 2010, 10:39 pm

Re: New to C# and can't figure out how to organize classes

Postby garysimmons » October 5th, 2011, 9:52 am

As I understood it, at least the past...static methods were always allowed but static members were not. Don't know if that's changed in the latest release though.
garysimmons
 
Posts: 99
Joined: May 15th, 2011, 12:02 pm


Return to C#