Help with objects in C#

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
I have a school group project this semester and we have to create a Fantasy Football Mock Draft in Visual C#.

I am thinking that the best way to manage each player's stats is by creating an object class called "Player" or something similar. Then, create an array like so:
Code:
Player myPlayer[] = new Player[100];  //arbitrarily picked 100

I know how to use basic variables for the object, things like:

Code:
myPlayer[5].Name = "Joe Schmoe"; 
myPlayer[5].Position = "QB";
and so forth...

But what I would really like to do is be able to use things like:
Code:
myPlayer[5].Passing.Yards = 150;
myPlayer[5].Passing.TDs = 1;
myPlayer[5].Rushing.Yards = 48;
myPlayer[5].Rushing.TDs = 2;

In other words, have "categories" for certain variables, and be able to create and use variables as a subset of said categories. Is this even possible in C#? I tried Googling a few things, but since I don't really know the correct terminology to search for, I am having trouble finding any answers. How would I go about creating these variables in this manner?

Thanks in advance for anyone who can shed some light on this.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
looks like you want an object in an object. What you need to do is create a passing object, give it the Yards, TDs, and whatever else and then give your player object a passing object. (named Passing if you like).
 

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
looks like you want an object in an object. What you need to do is create a passing object, give it the Yards, TDs, and whatever else and then give your player object a passing object. (named Passing if you like).

So how do I create the 2nd object? I can't create a new class "Passing" or whatever inside of the "Player" class, correct?
 

ioni

Senior member
Aug 3, 2009
619
11
81
So how do I create the 2nd object? I can't create a new class "Passing" or whatever inside of the "Player" class, correct?

You can, although it's not recommended. Make a new file for every class.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
You can, although it's not recommended. Make a new file for every class.

if these new classes will only ever be used with the player class, I think keeping them in the same file is not a bad thing. It is when the classes will be used elsewhere or they will have a lot of code that they really need their own files.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
You should avoid arrays:

Player myPlayer[] = new Player[100]; //arbitrarily picked 100

They are sloppy and slow to work with. Especially if you have to resize the array.

I suggest looking up:

System.Collections.Generic.List(of T)

They are much cleaner to work with.

http://www.csharp-station.com/Tutorials/Lesson20.aspx

But if you are doing this for school and haven't covered collections, and only arrays, maybe sticking with arrays is a better choice.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Yea, small private container classes I don't consider bad to have in the same file.

Basicly you want to make a private member variable and define the getter/setter for it (the link someone else gave probably goes into detail on how haven't check though).

On a side note, you probably want a list<> or arraylist to hold players assuming you don't know the exact amount of players.
 

douglasb

Diamond Member
Apr 11, 2005
3,157
0
76
I have had a small amount of success creating multiple classes in the same file, then creating one instance of each "subclass" in my main Player class.

I like the list idea as well. Thanks for the help so far, guys.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
You should avoid arrays:

Player myPlayer[] = new Player[100]; //arbitrarily picked 100

They are sloppy and slow to work with. Especially if you have to resize the array.

I suggest looking up:

System.Collections.Generic.List(of T)

They are much cleaner to work with.

http://www.csharp-station.com/Tutorials/Lesson20.aspx

But if you are doing this for school and haven't covered collections, and only arrays, maybe sticking with arrays is a better choice.
Arrays are slow in C#? That is interesting. In C and C++, arrays are awesome (so long as you don't have to grow them).
 

Rangoric

Senior member
Apr 5, 2006
530
0
71
Arrays are slow in C#? That is interesting. In C and C++, arrays are awesome (so long as you don't have to grow them).

Collections are implemented via Arrays internally actually. That double in size whenever they need to grow. (Typically)

Arrays are pretty quick in C#, not sure why he said they aren't. I mean, they aren't nearly as easy to use as a List<TType>, but sometimes you know exactly how many widgets you need in a group, and it can work out faster.

I stick to Lists and Enumerables and only use arrays when I need to, such as with Streams.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Collections are implemented via Arrays internally actually. That double in size whenever they need to grow. (Typically)

Arrays are pretty quick in C#, not sure why he said they aren't. I mean, they aren't nearly as easy to use as a List<TType>, but sometimes you know exactly how many widgets you need in a group, and it can work out faster.

I stick to Lists and Enumerables and only use arrays when I need to, such as with Streams.

perhaps by "slow to work with" he meant that they take more programmer time.
 

Krioni

Golden Member
Feb 4, 2000
1,371
0
71
Yeah, I don't get the slow comment either. Arrays and Lists have their place. If you don't have to resize the array at run time, the array is probably faster than a list. Lists are much easier to work with if you are dynamically adding/removing objects from the underlying array. Linq ends up making arrays pretty easy to query too.
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
I have a school group project this semester and we have to create a Fantasy Football Mock Draft in Visual C#.

I am thinking that the best way to manage each player's stats is by creating an object class called "Player" or something similar. Then, create an array like so:
Code:
Player myPlayer[] = new Player[100];  //arbitrarily picked 100

I know how to use basic variables for the object, things like:

Code:
myPlayer[5].Name = "Joe Schmoe"; 
myPlayer[5].Position = "QB";
and so forth...

But what I would really like to do is be able to use things like:
Code:
myPlayer[5].Passing.Yards = 150;
myPlayer[5].Passing.TDs = 1;
myPlayer[5].Rushing.Yards = 48;
myPlayer[5].Rushing.TDs = 2;

In other words, have "categories" for certain variables, and be able to create and use variables as a subset of said categories. Is this even possible in C#? I tried Googling a few things, but since I don't really know the correct terminology to search for, I am having trouble finding any answers. How would I go about creating these variables in this manner?

Thanks in advance for anyone who can shed some light on this.

An object can have other objects as properties/fields.

So what you really want to do is create a couple of stat objects. If you really want to spend time with it, you can probably find some shared attributes of stats. Common measurements such as yards might be able to be put into a base class and then inherited for specific stats (punting, passing, running). You might also think about the same for players. Offensive and defensive players have different stats, although this can get somewhat blurry (for instance, when a qb throws an INT and tackles the defensive back). Do you want to keep track of that kind of stat?

Code:
public class PassingStats
{
  public int Yards {get;set;}
  public int TDs {get;set;}
}

public class Player
{
  public PassingStats Passing = new PassingStats();
}

That is basic and will get you the basic functionality you want. You might also think about inheritance and making specific types for your positions if there are specific stats you want to track, or if you want to stick with a single player class make position an enum. It will save you many headaches.