【问题标题】:Dynamically convert an instance array with multiple values动态转换具有多个值的实例数组
【发布时间】:2018-11-07 03:23:14
【问题描述】:

令人惊讶的是,当前使用 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");
            }


    } 

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

【问题讨论】:

  • 为什么不直接使用List&lt;T&gt;

标签: c#


【解决方案1】:

使用List&lt;T&gt;。这是存储对象数组的好方法。

声明:

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


    } 

【讨论】:

  • 从存储对象数组的列表中访问元素的语法类似吗?与所有列表一样,仍然可以通过 Scores[number] 访问该列表吗?当前提供的解决方案将在继续读取时覆盖每个值
  • 您可以使用[] 访问List&lt;T&gt; 中的元素
  • @HARVmackie 是的,您可以像访问数组 Scores[9].ScoreTurns 一样访问列表中的对象。
  • @HARVmackie 为什么说这些值被覆盖了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 2020-09-27
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多