RePRO
RePRO

Reputation: 67

How to move button in array and set null of come from?

I have the multi-dimensional array originalArray[X_VECTOR, Y_VECTOR] of MyButtons.

MyButton is simple created class (maybe unnecessary):

class MyButton : Button
{
    private int[] id;

    public MyButton()
    {
        id = new int[2];
    }

    public int[] ID
    {
        get
        {
            return id;
        }

        set
        {
            id = value;
        }
    }
}

In loop we fill the array of buttons:

public void fillArray() {
    originalArray = new MyButton[X_VECTOR, Y_VECTOR];
    int count_buttons = 0;
    for (int i = 0; i < X_VECTOR; ++i)
    {
        for (int j = 0; j < Y_VECTOR; ++j)
        {
            count_buttons++;
            MyButton btn = new MyButton();
            btn.Name = "btn " + count_buttons;
            btn.ID[0] = i;
            btn.ID[1] = j;
            originalArray[i, j] = btn;   
        }
     }
}

Now, we would like to move button to right side after click in array:

protected void MyBtnClick(object sender, EventArgs e) {
    if (sender != null) {
        MyButton myclickbutton = (MyButton)sender;
        int x = myclickbutton.ID[0];
        int y = myclickbutton.ID[1];

        MyButton temp = originalArray[x, y];
        temp.Location = new Point(curr_pos_x + 55, curr_pos_y);
        temp.ID[0] = x; 
        temp.ID[1] = y + 1; // new coordinate y
        originalArray[x, y + 1] = temp;
        temp = null;
        // originalArray[x, y] = null;
    }
}

NULL is not set. What I'm going wrong?

I need this ilustration:

BEFORE CLICK:

originalArray[0,0] = btn instance; 
originalArray[0,1] = null;

AFTER CLICK:

originalArray[0,0] = null; 
originalArray[0,1] = btn instance;

EDIT: When I tried this:

protected void MyBtnClick(object sender, EventArgs e) {
    if (sender != null) {
        MyButton myclickbutton = (MyButton)sender;
        int x = myclickbutton.ID[0];
        int y = myclickbutton.ID[1];

        myclickbutton.Location = new Point(curr_pos_x + 55, curr_pos_y);
        myclickbutton.ID[0] = x;
        myclickbutton.ID[1] = y + 1;
        originalArray[x, y + 1] = myclickbutton;
        originalArray[x, y] = null;
    }
}

That maybe OK, but when I was testing this

if ((originalArray[i, j].Name == testArray[i, j].Name)) ...

This line gets me NullReferenceException.

This function I same like fillArray above, and this I call in constructor:

public void createTestArray() {
        testArray = new MyButton[X_VECTOR, Y_VECTOR];
        int count_buttons = 0;
        for (int i = 0; i < X_VECTOR; ++i)
        {
            for (int j = 0; j < Y_VECTOR; ++j)
            {
                count_buttons++;
                MyButton btn = new MyButton();
                btn.Name = "btn " + count_buttons;
                testArray[i, j] = btn;   
            }
         }
}

Upvotes: 2

Views: 220

Answers (2)

Viktor S.
Viktor S.

Reputation: 12815

You made one of items of array to be null, now you go through all of them, null items throw you an exception. Just check if item is not null before names are compared and you will not get an exception

Upvotes: 1

christopher
christopher

Reputation: 27346

You're getting a message saying Null is not set. You have commented out the line that returns the original x and y entry in the multi-dimensional array to null. What you've got at the moment is:

originalArray[0,0] = btn instance; 
originalArray[0,1] = temp;

Remove the comments from your last line and you should erase btn instance in 0,0.

Upvotes: 0

Related Questions