I'm working on a leaderboard for a game I am making.
- In this code I create a list called 'players', it is a list of objects from a custom class called 'Player'.
- The code then reads all of the highscores from a file (which has already been created), line by line. (Each line takes the format of "Name Age Gender Score").
- For each line it splits up the text into 4 parts and assigns each of those parts to a variable.
- Then it adds these variables to the list 'players'.
The problem I am having is that all of the items in the list end up with the same values.(The values on the last line of my highscores file). Any ideas how to fix this?
Here is the code:
private void LeaderboardScreen_Load(object sender, EventArgs e)
{
string name = "error";
string age = "error";
string gender = "error";
int score = 0;
List<Player> players = new List<Player>();
using (var fileStream = File.OpenRead(".\\InputInfo.bin"))
using (var streamReader = new StreamReader(fileStream, true))
{
string line;
line = streamReader.ReadLine();
}
foreach (var line in File.ReadLines(".\\InputInfo.bin")) // loop for every line in the inputinfo file
{
string[] words = line.Split(); //Splits the line into seperate words, and puts them into an array called 'words'
name = words[0]; // Makes the first word the name
age = words[1]; //Makes the second word the age
gender = words[2];//Makes the third word the gender
score = Convert.ToInt32(words[3]);//Makes the forth word the score
players.Add(new Player(name,age,gender,score)); **//This is where the problem is I think**
}
players = players.OrderByDescending(i => score).ToList(); //sorts list of players into descending order
}
Also if it helps at all here is the class:
public class Player
{
public static int score = 0;
public static string name;
public static string age;
public static string gender;
public Player(string aName, string aAge, string aGender, int aScore)
{
name = aName;
age = aAge;
gender = aGender;
score = aScore;
}
}