我有一堂课,里面有一个图片框。我已经创建了一个类的对象数组,但是当我尝试创建一个新的图片框时,我收到一条错误消息,指出对象引用未设置为对象的实例。
这是我创建图片框的方式
GreyHound[] dog;
public Form1()
{
InitializeComponent();
dog[1].MyPictureBox = new PictureBox();
dog[1].MyPictureBox = pictureBox1;
dog[2].MyPictureBox = new PictureBox();
dog[2].MyPictureBox = pictureBox2;
dog[3].MyPictureBox = new PictureBox();
dog[3].MyPictureBox = pictureBox3;
dog[4].MyPictureBox = new PictureBox();
dog[4].MyPictureBox = pictureBox4;
编辑代码:
灰狗[] 狗;
public Form1()
{
InitializeComponent();
dog = new GreyHound[4];
dog[0].MyPictureBox = new PictureBox();
dog[0].MyPictureBox = pictureBox1;
dog[1].MyPictureBox = new PictureBox();
dog[1].MyPictureBox = pictureBox2;
dog[2].MyPictureBox = new PictureBox();
dog[2].MyPictureBox = pictureBox3;
dog[3].MyPictureBox = new PictureBox();
dog[3].MyPictureBox = pictureBox4;
}