首页 > 解决方案 > 动态转换具有多个值的实例数组

问题描述

目前使用 Scores 类(显示下面变量的代码)确实会存储用户分数。由于 C# 中不存在向量,分数当前保存在预定义(大小)数组中。我研究并发现有两种数据结构(可能更多我没有找到但很可能不相关)用于将静态数组转换为动态集。

链表和数组列表。

从研究来看,链接列表似乎是大多数人的偏好。

public class Score
    {
        private int _ScoreId;
        public int ScoreId
        {
            get { return _ScoreId; }
            set
            {
                _ScoreId = value;
            }
        }
        private string _ScoreUsername;
        public string ScoreUsername
        {
            get { return _ScoreUsername; }
            set
            {
                if (value.Length >= 5 || value.Length <= 10)
                {
                    _ScoreUsername = value;
                }
                else
                {
                    throw new Exception("The Username must be between 5 - 10 Characters");
                }
            }
        }
        private int _Turns;
        public int ScoreTurns
        {
            get { return _Turns; }
            set
            {
                if (_Turns >= 0)
                {
                    _Turns = value;
                }
                else
                {
                    throw new Exception("Invalid Turns Entry - Must have completed 1 turn");
                }
            }

        }
    }

当前实例数组初始化:

Score[] Scores = new Score[10];

使用 Scores 类的代码

 private void InsertScores(int scoreId, int scoreValue, string Username)
    {

            //Connection
            Connection();
            //Declare Object
            for (int Id = 0; Id < Scores.Length; Id++)
            {
                Scores[Id] = new Score();
            }

                //Select All rows and populate object instance
                SqlCommand cmd = new SqlCommand("SELECT * FROM gameScores Order By scoreValue ASC", Con);
                int Element = 0;
                //data reader
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Scores[Element].ScoreId = Convert.ToInt32(rdr[0].ToString());
                    Scores[Element].ScoreTurns = Convert.ToInt32(rdr[1].ToString());
                    Scores[Element].ScoreUsername = rdr[2].ToString();
                    Element++;
                }
                rdr.Close();

            //int ScoreId = 9;
            if (scoreValue < Scores[9].ScoreTurns)
            {
                SqlCommand sql = new SqlCommand("UPDATE gameScores SET scoreValue = @scoreValue, username = @Username WHERE scoreid = @ScoreId;", Con);
                sql.Parameters.AddWithValue("@scoreValue", scoreValue);
                sql.Parameters.AddWithValue("@Username", Username);
                sql.Parameters.AddWithValue("@ScoreId", Scores[9].ScoreId);
                //Insert
                sql.ExecuteNonQuery();
            }
            else
            {
                MessageBox.Show("You sadly have not made the High Scores Leaderboard");
            }


    } 

有没有人在使用实例数组时将静态数组转换为链表?如果是这样,您采取了哪些步骤,还没有看到太多使用对象数组的链表进行在线记录

标签: c#

解决方案


使用List<T>. 这是存储对象数组的好方法。

宣言 :

List<Score> Scores = new List<Score>();

用法 :

private void InsertScores(int scoreId, int scoreValue, string Username)
    {

            //Connection
            Connection();

                //Select All rows and populate object instance
                SqlCommand cmd = new SqlCommand("SELECT * FROM gameScores Order By scoreValue ASC", Con);
                //data reader
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Score score = new Score(); //Create object to store the data
                    score.ScoreId = Convert.ToInt32(rdr[0].ToString());
                    score.ScoreTurns = Convert.ToInt32(rdr[1].ToString());
                    score.ScoreUsername = rdr[2].ToString();
                    Scores.Add(score); //Add the object to the array
                }
                rdr.Close();

            //int ScoreId = 9;
            if (scoreValue < Scores[9].ScoreTurns)
            {
                SqlCommand sql = new SqlCommand("UPDATE gameScores SET scoreValue = @scoreValue, username = @Username WHERE scoreid = @ScoreId;", Con);
                sql.Parameters.AddWithValue("@scoreValue", scoreValue);
                sql.Parameters.AddWithValue("@Username", Username);
                sql.Parameters.AddWithValue("@ScoreId", Scores[9].ScoreId);
                //Insert
                sql.ExecuteNonQuery();
            }
            else
            {
                MessageBox.Show("You sadly have not made the High Scores Leaderboard");
            }


    } 

推荐阅读