开发者

whats the problem of this code?

I wrote some code to move the king in chess game; can you tell me where is the problem of this code that king doesn't move? Thanks.

EDITED2:

public partial class Form1 : Form
{
    PictureBox[,] pic = new PictureBox[8, 8];

private void pictureBox34_Click(object sender, EventArgs e)
    {

if (pictureBox34.Image == chess9.Properties.Resources.siyahsah2)

{

f();
}
}

public void picarray()
{
        pic[0, 0] = pictureBox54;
        pic[0, 1] = pictureBox64;
        pic[0, 2] = pictureBox48;
        pic[0, 3] = pictureBox42;
        pic[0, 4] = pictureBox34;
        pic[0, 5] = pictureBox26;
        pic[0, 6] = pictureBox18;
        pic[0, 7] = pictureBox8;
        pic[1, 0] = pictureBox1;
        pic[1, 1] = pictureBox2;
        pic[1, 2] = pictureBox3;
        pic[1, 3] = pictureBox4;
              .
              .///thats so long(64 arrays)
              .
 }

public void f()
{



        int x = 3;
        int y = 3;

       for (int i = 1; i < x; i++)
       {
            for (int j = 1; j < y; j++)
            {
                pic[i, j] = new PictureBox();

                pic[i, j] = pic[i + 1, j + 1];

                pic[i, j] = new PictureBox();
                pic[i, j].Image = Image.FromFile("pic/siyahsah2.jpg");

          }
开发者_JS百科
}
}


if (pictureBox34.Image == Image.FromFile("pic/siyahsah2.jpg"))

Do you know the implications of this line of code?

  • Every time this statement is executed, a new instance is created, using the image stored on disk
  • The comparison never evaluates to true, since the comparison is based on the equality of references, which obviously cannot be true for a newly created object


No, you didn't define the return types correctly, as you don't have any return values at all anywhere in that code.

Let's see what the code might do...

The first problem is this line:

if (pictureBox34.Image == Image.FromFile("pic/siyahsah2.jpg"))

The FromFile method will create a new object, and as that object will never be the same object as the one stored in the picture box, the condition is always false and the method f is never called.

In the method picarray you are using the variable pic, but that won't be the same variable as the one used in the f method, as that variable is declared locally in that method,

In the f method you are declaring an array of picture boxes which you manipulate, but then you just exit the method without doing anything with the array, so the array just goes away and the result will never be visible anywhere.

As the array is newly created, it contains only null referecens, so copying them from one item to another in the array doesn't accomplish anything. You are also copying items from two positions into the same position, so the second copy will overwrite the first.

As the variables i and j are set to zero, [i - 1, j - 1] will try to access an item that is outside the array, which would give you an exception.

You are trying to store something in the Image property of one of the items in the array, but as all items in the array are null, there is no picture box that you can set the Image property of.

It's hard to tell what you are trying to do, but this information should at least help you to understand what the code doesn't do.


Its hard to tell what you are trying to do in the overall code with this snippet but it doesnt look right, no.

The problem I see is that in f() you create the PictureBox pic, set some of its properties but then dont do anything with it. At the end of f() the method returns and pic is destroyed.

If this code actually compiles then from your use of pix[] in the picarray() method I would say you have a class level variable somewhere that is your game board. In that case you dont need the line:

PictureBox[,] pic = new PictureBox[8, 8];

in f() because its just creating a new empty board that exists only within f() rather than updating your real board.


Remove this line

PictureBox[,] pic = new PictureBox[8, 8]; from the f() function

i think , you are again initilizing of pic array with new picture

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜