我的对象遇到了一些困难。我试图拥有一个我创建的对象(问题)数组:
public class Question
{
public int id = 0;
public string question = string.Empty;
public string a1 = string.Empty;
public string a2 = string.Empty;
public string a3 = string.Empty;
public string a4 = string.Empty;
public int Tanswer = 0;
}
现在我试图在其中设置一些值,如下所示:
Question[] arr = new Question[4];
Random rnd = new Random();
int i = 0;
int temp = 0;
bool ok = false;
while (i < 3)
{
temp = rnd.Next(0, Int32.Parse(dt_count.Rows[0][0].ToString()) - 1);
for (int j = 0; j < arr.Length; j++)
{
arr[j].id = Int32.Parse(dt_q_notMust.Rows[temp][0].ToString()); // ID
arr[j].question = dt_q_notMust.Rows[temp][1].ToString(); // Question
arr[j].a1 = dt_q_notMust.Rows[temp][2].ToString(); // A1
arr[j].a2 = dt_q_notMust.Rows[temp][3].ToString(); // A2
arr[j].a3 = dt_q_notMust.Rows[temp][4].ToString(); // A3
arr[j].a4 = dt_q_notMust.Rows[temp][5].ToString(); // A4
arr[j].Tanswer = Int32.Parse(dt_q_notMust.Rows[temp][6].ToString()); // True Answer (int).
if (arr[j].id != temp)
{
ok = true;
}
}
if (ok)
{
i++;
}
}
并且由于某种原因它写了一个错误,就像我从来没有初始化它一样:
你调用的对象是空的。
但我确实写了:
Question[] arr = new Question[4];
所以我想知道问题是什么?
谢谢!