0

我是 C# 新手,并且在 C++ 方面有同样的经验(大学一年级编程)。最近我们开始使用 C#,而我想做的是带有印刷板的简单游戏(到目前为止,我只使用控制台应用程序)。我开始挖掘(我将在下面放置主题的链接)。

我正在使用 Visual Studio Ultimate 2013。我想要制作的板是简单的按钮板(但具有额外的属性)。所以我创建了一个从按钮继承的类并开始添加一些东西(会有更多):

class Field : Button
{
    private string owner;
    private string temp_owner;
    private int level;
    private int temp_level;

    public Field() : base()
    {
        owner = "brak";
        temp_owner = owner;
        level = 0;
        temp_level = level;
    }
}

然后我查看了按钮的初始化方式(在 Form1.Designer.cs 中),但 Visual Studio 不允许我在那里写任何东西,所以我创建了方法 build_board 并将其放在 Form1.cs 中:

public partial class main_form : Form
{
    public main_form()
    {
        InitializeComponent();
        build_board();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private System.Windows.Forms.Button [,] Board;
    private void build_board()
    {
        this.Board = new Field[10, 10];
        this.SuspendLayout();
        //
        // Board
        //
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
            {
                this.Board[i, j].BackColor = System.Drawing.SystemColors.Control;
                this.Board[i, j].Location = new System.Drawing.Point(10 + i * 10, 10 + j * 10);
                this.Board[i, j].Name = "Field [ " + i + " , " + j + " ]";
                this.Board[i, j].Size = new System.Drawing.Size(50, 50);
                this.Board[i, j].TabIndex = 100 * i + j;
                this.Board[i, j].Text = "";
                this.Board[i, j].UseVisualStyleBackColor = false;
            }

        this.ResumeLayout(false);
    }
}

问题是调试器在这一行中尖叫着“NullReferenceException was unhandled”:

this.Board[i, j].BackColor = System.Drawing.SystemColors.Control;

我检查了其他一些主题,他们说这可能是由于 Board 未初始化造成的,但我认为我确实对其进行了初始化。我将不胜感激任何帮助,也许还有一些关于如何轻松打印自定义对象数组的其他提示。

我的研究:

C# NullReferenceException 未处理 - 对象引用未设置为对象的实例

NullReferenceException 未处理 C#

如何在 C# 中创建自定义类型的数组?

(还有许多其他关于打印的)

编辑:

Pazi01 的回答:在循环中需要添加这两行:

    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
        {
            this.Board[i, j] = new Field();
            this.Controls.Add(this.Board[i, j]);
            ...
        }
4

1 回答 1

0

原因是您没有初始化数组中的数据

        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
            {
                this.Board[i, j] = new Field();
                this.Board[i, j].BackColor = System.Drawing.SystemColors.Control;
                this.Board[i, j].Location = new System.Drawing.Point(10 + i * 10, 10 + j * 10);
                this.Board[i, j].Name = "Field [ " + i + " , " + j + " ]";
                this.Board[i, j].Size = new System.Drawing.Size(50, 50);
                this.Board[i, j].TabIndex = 100 * i + j;
                this.Board[i, j].Text = "";
                this.Board[i, j].UseVisualStyleBackColor = false;
            }

我插入了这个:

            this.Board[i, j] = new Field();

编辑: 您还必须将控件添加到您的表单

this.Controls.Add(this.Board[i, j]);
于 2015-04-03T12:23:08.320 回答