0

我的对象遇到了一些困难。我试图拥有一个我创建的对象(问题)数组:

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];

所以我想知道问题是什么?

谢谢!

4

6 回答 6

4

您已经初始化arr为一个新数组,但它的所有元素仍然为空。您需要在访问每个项目之前对其进行初始化:

arr[j] = new Question();
arr[j].id = Int32.Parse(dt_q_notMust.Rows[temp][0].ToString());
...
于 2012-12-04T13:00:17.643 回答
1

您创建的数组填充了空值,而不是 Question 实例。

arr[0] = new Question();
arr[1] = new Question();
arr[2] = new Question();
arr[3] = new Question();

这可以解决问题。

于 2012-12-04T12:59:55.850 回答
1

您必须创建数组的每个对象。

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++;
        }
    }
于 2012-12-04T13:01:23.050 回答
0

我认为您已经初始化了数组(实际上,您为 4 个项目分配了一些内存),但没有初始化数组中的对象。我现在没有 Visual Studio 来尝试解决方案,但您可以尝试使用已初始化的对象来初始化数组。

于 2012-12-04T13:01:08.987 回答
0

所以在这里你只需创建一个“数组”实例,它可以包含“问题”类的实例。但是您还没有创建任何“问题”实例。

您可以添加以下行:

arr[j] = new Question();

在设置属性值之前。

于 2012-12-04T13:07:48.007 回答
0

正如其他人所说,虽然您已经初始化了数组,但您还没有初始化Array. 如果将 aQuestion视为值类型更有意义,您可以将其定义为 astruct而不是 a class,这样初始化Array单独就足够了,您可以访问其中的项目以根据需要设置它们的属性,而无需初始化项目。但是,如果您想保留Question为一个类,则可以将 包装Array在一个类中,该类在访问项目时会延迟初始化它们。

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];
    }
}

然后您可以按如下方式使用它:

var questions = new SelfInitializingArray<Question>(4, () => new Question());

questions[0].Id = 12345; //etc.
于 2012-12-04T14:06:46.417 回答