0

这就是我试图实现我的 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 等的相同错误。

在这种情况下如何正确声明数组?

4

1 回答 1

1

上面的声明本身没有问题,但是如果你在一个类中声明字段,你就不能这样使用它。如错误所述,字段初始化程序(在您的情况下columns)不能引用其他非静态字段(在您的情况下column0column1...)。

您可以采取 2 种方法:

要么不声明中间字段,而是一次性完成(最好的,除非你真的需要直接引用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 };
}
于 2012-12-08T05:33:19.113 回答