C# 对象引用未设置为对象的实例。-但我确实初始化了它?
我的目标遇到了一些困难。 我正在尝试创建一个对象数组(问题):C# 对象引用未设置为对象的实例。-但我确实初始化了它?,c#,C#,我的目标遇到了一些困难。 我正在尝试创建一个对象数组(问题): 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 strin
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++;
}
}
所以我想知道问题是什么
谢谢 您创建的数组中填充的是空值,而不是问题实例
这就可以了。您创建的数组中填充的是空值,而不是问题实例
这就可以了。您已经初始化了
arr
作为一个新数组,但它的所有元素仍然为空。您需要在访问每个项目之前对其进行初始化:
arr[j] = new Question();
arr[j].id = Int32.Parse(dt_q_notMust.Rows[temp][0].ToString());
...
您已将
arr
初始化为一个新数组,但其所有元素仍然为空。您需要在访问每个项目之前对其进行初始化:
arr[j] = new Question();
arr[j].id = Int32.Parse(dt_q_notMust.Rows[temp][0].ToString());
...
我认为您已经初始化了数组(实际上,您为4个项目分配了一些内存),但没有初始化数组中的对象。我现在没有visual studio来尝试该解决方案,但您可以尝试使用已初始化的对象初始化数组。我认为您已初始化数组(实际上,您为4个项目分配了一些内存),但未初始化数组中的对象。我现在没有visual studio来尝试该解决方案,但您可以尝试使用已初始化的对象初始化数组。您必须创建数组的每个对象
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] = new Question();
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=新问题[4];
随机rnd=新随机();
int i=0;
内部温度=0;
bool ok=false;
而(i<3)
{
temp=rnd.Next(0,Int32.Parse(dt_count.Rows[0][0].ToString())-1);
对于(int j=0;j
必须创建数组的每个对象
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] = new Question();
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=新问题[4];
随机rnd=新随机();
int i=0;
内部温度=0;
bool ok=false;
而(i<3)
{
temp=rnd.Next(0,Int32.Parse(dt_count.Rows[0][0].ToString())-1);
对于(int j=0;j
因此,这里您只需创建一个“Array”实例,它可以包含类“Question”的实例。但您还没有创建任何“问题”实例
您可以添加以下行:
arr[j] = new Question();
在设置属性值之前。因此,这里您只需创建一个“Array”实例,它可以包含类“Question”的实例。但您还没有创建任何“问题”实例 您可以添加以下行:
arr[j] = new Question();
在设置属性值之前。如其他人所述,当您初始化数组时,您没有初始化
数组中的项。如果将问题
视为值类型更有意义,则可以将其定义为结构
而不是类
,这样仅初始化数组
就足够了,您可以访问其中的项,以根据需要设置其属性,而无需初始化项。但是,如果希望将Question
作为一个类保留,则可以将数组
封装在一个类中,该类在访问项时惰性地初始化它们
public class SelfInitializingArray<T> : IEnumerable<T> where T : class
{
private Func<T> _builder;
private T[] _items;
/// <summary>
/// Initializes a new instance of the SelfInitializingArray class.
/// </summary>
public SelfInitializingArray(int size, Func<T> builder)
{
_builder = builder;
_items = new T[size];
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < _items.Length; i++)
{
yield return ItemOrDefaultAtIndex(i);
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public T this[int index]
{
get
{
return ItemOrDefaultAtIndex(index);
}
set
{
_items[index] = value;
}
}
private T ItemOrDefaultAtIndex(int index)
{
if (_items[index] == null)
_items[index] = _builder();
return _items[index];
}
}
公共类自初始化数组:IEnumerable其中T:class
{
私人职能建筑商;
私人T[]_项目;
///
///初始化SelfInitializingArray类的新实例。
///
公共自初始化数组(整数大小,函数生成器)
{
_建筑商=建筑商;
_项目=新T[尺寸];
}
公共IEnumerator GetEnumerator()
{
对于(int i=0;i<\u items.Length;i++)
{
收益回报项目收益率指数(i);
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回此.GetEnumerator();
}
公共T此[int索引]
{
得到
{
返回项默认索引(索引);
}
设置
{
_项目[指数]=价值;
}
}
私有T项默认索引(整数索引)
{
if(_items[索引]==null)
_项目[索引]=_builder();
返回项目[索引];
}
}
然后,您可以按如下方式使用它:
var questions = new SelfInitializingArray<Question>(4, () => new Question());
questions[0].Id = 12345; //etc.
var-questions=new-selfinitializengarray(4,()=>new-Question());
问题[0]。Id=12345//等
如其他人所述,虽然您已初始化数组,但尚未初始化数组中的项。如果将问题
视为值类型更有意义,您可以