Reputation: 23
First post, i'm very new to C# and programming in general.
I'm making a football player generator application solely for practise purposes, where i have a class with a constructor that generates an object with two strings (firstname and last name) and a lot of ints. (different skill attributes, like shooting power, passing etc.
Bear with me, i'll post my chucks of code in a sec.
So i want all of these objects im making to be stored with all its variables in some kind of way. And after a great deal of googling, im down to arrays of different sorts, lists and dictionaries.
first off, this is my class:
class player
{
public string firstName;
public string lastName;
public string playPos;
public int playerId = 999;
public int isKeeper;
public int level;
public int age;
public int dKeeper;
public int dTackle;
public int dMarking;
public int mPlaymaking;
public int mCrossing;
public int fShooting;
public int fShotPower;
public int aDribbling;
public int aHeading;
public int aPassing;
public void autoGenPlayer()
{
//new killing random that i got off of Stack Overflow
Random rAG = new Random(Guid.NewGuid().GetHashCode());
//PlayerId, dont mind it does not work properly yet.
playerId = playerId + 1;
//a level integer just to base the other stats around, to keep the stats from spreading too much
level = rAG.Next(2, 11);
//Generating names, calling the methods a bit lower in this class.
firstName = firstNameGenerator();
lastName = lastNameGenerator();
//giving the player an age
age = rAG.Next(16, 35);
//a one out of seven chance to become a goal keeper.
isKeeper = rAG.Next(1,8);
//skills based on level. Max value is 20
dTackle = rAG.Next(level, level * 2);
dMarking = rAG.Next(level, level * 2);
mPlaymaking = rAG.Next(level, level * 2);
mCrossing = rAG.Next(level, level * 2);
fShooting = rAG.Next(level, level * 2);
fShotPower = rAG.Next(level, level * 2);
aDribbling = rAG.Next(level, level * 2);
aHeading = rAG.Next(level, level * 2);
aPassing = rAG.Next(level, level * 2);
//Checks if the player is a keeper. 5 is chosen at random. There are obv better ways to do this, but it doesnt matter right now.
if (isKeeper == 5)
{
//This basically just makes the keeper a keeper, and a shit outfield player.
dKeeper = rAG.Next(level, level * 2);
playPos = "Goal Keeper";
dTackle = rAG.Next(1, 3);
dMarking = rAG.Next(1, 3);
mPlaymaking = rAG.Next(1, 3);
mCrossing = rAG.Next(1, 3);
fShooting = rAG.Next(1, 3);
fShotPower = rAG.Next(1, 3);
aDribbling = rAG.Next(1, 3);
aHeading = rAG.Next(1, 3);
aPassing = rAG.Next(1, 3);
}
else
{
//if not a keeper, shit keeper attributes, and random outfielder atts.
dKeeper = 1;
}
//my clever way of assigning a player position to the players.
int def = dTackle + dMarking;
int mid = mCrossing + mPlaymaking;
int fwd = fShooting + fShotPower;
if (dKeeper > 1)
{
playPos = "Goal Keeper";
}
else if (def >= fwd && def >= mid)
{
playPos = "Defender";
}
else if (mid >= fwd && mid >= def)
{
playPos = "Midfielder";
}
else if (fwd >= mid && fwd >= def)
{
playPos = "Striker";
}
else
{
//in a rare case (if ever) the logic doesnt add ut, im spawning a star player. because. im not too got at this.
playPos = "Utility Legend";
dTackle = rAG.Next(16, 21);
dMarking = rAG.Next(16, 21);
mPlaymaking = rAG.Next(16, 21);
mCrossing = rAG.Next(16, 21);
fShooting = rAG.Next(16, 21);
fShotPower = rAG.Next(16, 21);
aDribbling = rAG.Next(16, 21);
aHeading = rAG.Next(16, 21);
aPassing = rAG.Next(16, 21);
}
}
//Generates a first name
public string firstNameGenerator()
{
string returnfirstName;
string[] firstnames;
firstnames = new string[60] { "60 different strings of first names... took them out for you, becasue it looked bad in the editor." };
Random rF = new Random(Guid.NewGuid().GetHashCode());
returnfirstName = firstnames[rF.Next(0, 40)];
return returnfirstName;
}
//generates a last name
public string lastNameGenerator()
{
string returnlastName;
string[] lastnames;
lastnames = new string[60] { "60 different strings of lastnames........." };
Random rL = new Random(Guid.NewGuid().GetHashCode());
returnlastName = lastnames[rL.Next(0, 40)];
returnlastName = lastnames[rL.Next(0, 40)];
return returnlastName;
}
}
And now for my other code, you know - the part where you put everything together.
namespace FormManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
player p = new player();
p.autoGenPlayer();
textBox10.Text = p.firstName + " " + p.lastName;
textBox11.Text = p.age.ToString();
textBox1.Text = p.aPassing.ToString();
textBox2.Text = p.aDribbling.ToString();
textBox3.Text = p.aHeading.ToString();
textBox4.Text = p.dTackle.ToString();
textBox5.Text = p.dMarking.ToString();
textBox6.Text = p.fShooting.ToString();
textBox7.Text = p.fShotPower.ToString();
textBox8.Text = p.mPlaymaking.ToString();
textBox9.Text = p.mCrossing.ToString();
textBox12.Text = p.dKeeper.ToString();
textBox13.Text = p.playPos;
}
private void button2_Click(object sender, EventArgs e)
{
//number of objects to generate
int numberOfPlayersToGenerate = 10;
string[] savePlayers = new string[numberOfPlayersToGenerate];
//Generate many objects
for (int i = 0; i < numberOfPlayersToGenerate; i++)
{
player play = new player();
play.autoGenPlayer();
}
}
}
}
So its basically the for loop here that i want to use to store all the generated variables. the autoGenPlayer() method is generating alot of different values of both int and string, and i want to store them all so i can make a nice looking table of some sort to display it.
I would love any ideas on this, its making me crazy at the moment.
Upvotes: 2
Views: 752
Reputation: 2473
Welcome to the community.
You actually have a lot more options than the three you have named.
To get your head around this properly you need to understand Inheritance.
If you are looking to bind this "group" to a Datagrid.Datasource then it needs to be of a type that that will take. Looking at the documentation this is of type system.object (not a lot of help because everything inherits from system.object). However, if you read a little further there is a list of specific objects (all starting with "Data" and all quaintly obsolescent) and any component that implements the IListSource or IList interface.
The thing that makes all of these suitable is that Array, List(T) and Dictionary(TKey, TValue) all implement the IList interface.
So which should you use?
This question gives you good reasons that you should use the IList interface itself. That way you put only minimal constraints on the people who consume your data. Specifically, in your case it should be an IList(player) (or even better an IList(IPlayer) and create an IPlayer interface that Player implements).
For internal storage go with the List(player) as it is unbounded (unlike arrays) and doesn't have the overhead of the dictionary key (you don't seem to want to look up the player by name in this - if that is something you do want to do then consider using the name as the key in a dictionary)
Also, if you change the autoGenPlayer into a constructor you won't need the play.autoGenPlayer();
line
Upvotes: 0
Reputation: 613
In form1 just add
List<Player> players = new List<Player>();
Then in your button1_Click() you add
players.add(p);
in button2_Click()
players.add(play);
Then you can add a gridview to your form and make players the datasource - FIN
Not so fin -
How do I bind a GridView to a custom object? if you don't know how to set a datasource.
Upvotes: 1