简体   繁体   中英

C# Maintaining to update the content of a List

Ive got a problem trying to maintaining to update the content of a list. After completing the game the users details will be stored within the 1st position within the list however if another user plays the game the previous users details would be replaced with the new details.I think the problem arises when the highscores form button is clicked as it opens up a new form causing the content of the list to be set to null.

1st User: https://cdn.discordapp.com/attachments/176014540268371968/556088180466647061/unknown.png 2nd User: https://cdn.discordapp.com/attachments/176014540268371968/556088457311682609/unknown.png

Is there any possible solutions to this issue. Any Help would be appreciated.

public partial class MainMenu : Form
{
    private void HighScoresButton_Click(object sender, EventArgs e)
    {
        HighScoresMenu HighScoresMenu = new HighScoresMenu(newScore, newPoints, PlayersName);
        HighScoresMenu.Show();
    }

    public static List<Player> GetPlayers(float newScore, float newPoints, string PlayersName)
    {
        var players = new List<Player>();
        var newPlayer = new Player
        {
            Name = PlayersName,
            Points = newPoints,
            Timer = newScore
        };
        players.Add(newPlayer);
        var TopTenLevel1 = players.OrderByDescending(x => x.Timer).Take(10);
        return players;
    }  


public partial class HighScoresMenu : Form
{
    private void HighScoresMenu_Load(object sender, EventArgs e)
    {
        Debug.Print(county.ToString());

        foreach (Player players in MainMenu.GetPlayers(TempnewScore, 
        TempnewPoints, TempPlayersName))
        {
            string TemyPlayersName = "Player's Name";
            float point = 0;
            float time = 0.0f;
            List<string> Allplayers = new List<string>();
            for(int count = 1; count < 6;count++)
            {
             Allplayers.Add(string.Format("{0,-2:0}: {1,-40} : {2,9:0} Points : {3,9:0.0} secs", count, TemyPlayersName, point, time));
            }
            Allplayers[0] = (string.Format("{0,-2:0}: {1,-40} : {2,9:0} Points : {3,9:0.0} secs", 1, players.Name, players.Points, players.Timer));
            ListBoxLevel1.DataSource = Allplayers;
        }
     }
}

Okay, just to make sure I understood you correctly: you have a list of objects representing high-scores, and you want to update it.

What I suggest is:

  1. Create new high-score object for the player that has completed the game.
  2. Use List<>.Add() method to add it to the list (instead of replacing the object on index 0).
  3. Use List<>.Sort() method to sort the list by points.
  4. If list's length exceeds the desired maximum (say, if you want the high-scores table to have only 10 rows), remove the last elements in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM