我不知道为什么这不起作用
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Button[,] button = new Button[3, 3]{ {button1, button2, button3 },
{button4, button5, button6 },
{button7, button8, button9 } };
private void button_Click(object sender, EventArgs e)
{
}
}
我收到错误
所有9个按钮字段初始值设定项不能引用非静态字段
答案 0 :(得分:3)
字段初始值设定项(因为错误明确指出)不能引用非静态字段或值。 button1
到button9
不是静态的。要获得相同的结果,请在表单的构造函数中移动数组初始化:
private Button[,] button;
public Form1()
{
InitializeComponent();
button = new Button[3, 3]{ {button1, button2, button3 },
{button4, button5, button6 },
{button7, button8, button9 } };
}