这就是我尝试实现PictureBox数组的方法:
PictureBox[] column0 = new PictureBox[6];
PictureBox[] column1 = new PictureBox[6];
PictureBox[] column2 = new PictureBox[6];
PictureBox[] column3 = new PictureBox[6];
PictureBox[] column4 = new PictureBox[6];
PictureBox[] column5 = new PictureBox[6];
PictureBox[] column6 = new PictureBox[6];
PictureBox[][] columns =
new PictureBox[][]
{ column0, column1, column2, column3, column4, column5, column6 };
当我尝试制作数组数组时,我收到此错误:
字段初始值设定项无法引用非静态字段方法或属性“Connect_Four_Server.Server.column0”
和column1,column2等的相同错误
在这种情况下如何正确地声明数组数组?
答案 0 :(得分:1)
上述声明本身没有任何问题,但如果要在类中声明字段,则无法使用它。如错误所述,字段初始值设定项(在您的情况下为columns
)无法引用其他非静态字段(在您的情况下为column0
,column1
...)。
您可以采取两种方法:
要么不声明中间字段并将其全部组合在一起(除非您实际需要直接引用columnX
,否则最好):
PictureBox[][] columns =
new PictureBox[][]
{
new PictureBox[6],
new PictureBox[6],
new PictureBox[6],
new PictureBox[6],
new PictureBox[6],
new PictureBox[6],
new PictureBox[6]
};
或者将columns
的初始化放入构造函数中:
PictureBox[] column0 = new PictureBox[6];
PictureBox[] column1 = new PictureBox[6];
PictureBox[] column2 = new PictureBox[6];
PictureBox[] column3 = new PictureBox[6];
PictureBox[] column4 = new PictureBox[6];
PictureBox[] column5 = new PictureBox[6];
PictureBox[] column6 = new PictureBox[6];
PictureBox[][] columns;
public Server()
{
columns = new PictureBox[][] { column0, column1, column2, column3, column4, column5, column6 };
}