【发布时间】: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<T>?
标签: c#