动态转换具有多个值的实例数组

时间:2018-11-06 19:09:30

标签: c#

目前,令人惊讶的是,当前使用Scores类(显示以下变量的代码)来存储Users分数。由于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");
            }


    } 

有人使用实例数组将静态数组转换为链接列表吗?如果是这样,您采取了什么步骤,还没有看到太多使用对象数组链接列表的在线文档

1 个答案:

答案 0 :(得分:0)

使用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");
            }


    }