Reputation: 39
I'm working on a leaderboard for a game I am making.
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;
}
}
Upvotes: 0
Views: 112
Reputation: 161
It would help if you had included the file "InputInfo.bin". Could you please post that?
also you need to change this line:
players.Add(new Player(name,age,gender,score)); //This is where the problem is I think
to this:
players.Add(new Player(name,age,gender,score)); //** This is where the problem is I think**
It will not compile until the // are in front of the **
Upvotes: 0
Reputation: 28809
Remove static
. It signifies that your member is shared by all instances of the class, but you actually want them per-instance.
Upvotes: 3