0

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;
    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Andrew
  • 39
  • 1
  • 2
  • 4

2 Answers2

3

Remove static. It signifies that your member is shared by all instances of the class, but you actually want them per-instance.

Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85
  • That worked! I can't believe it was that simple, I've spent ages trying to find the problem. I'll write that down for future reference. – Andrew Feb 21 '15 at 22:47
  • @Andrew: [static](https://msdn.microsoft.com/library/98f28cdx) is not something you should even consider except under special circumstances... but what you definitely do want to know about are [properties](http://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties), rather than the instance fields you're now using. – Jeroen Mostert Feb 21 '15 at 23:00
0

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 **

John Miller
  • 151
  • 1
  • 13