Tacit
Tacit

Reputation: 908

Clearing button Array

I am creating a grid of buttons using the following code:

Button[][] buttons;

In the method:

for (int r = 0; r < row; r++)
    {
       for ( int c = 0; c < col; c++)
           {
             buttons[r][c] = new Button();
           }
    }

How can I clear and reset buttons[][] if row or col changes, is there away to do it?

Upvotes: 0

Views: 964

Answers (2)

Cody Gray
Cody Gray

Reputation: 244752

Yes, there is. You can call the Array.Clear() function. Since your array holds Button objects, which are reference types, it will reset every item in the array to null.

Array.Clear(buttons, 0, buttons.Length);

However, I strongly suggest using one of the generic containers for this, rather than a raw array. For example, a List<T> would be a good choice. In your case, T would be Button.

using System.Collections.Generic;  // required at the top of the file for List<T>

List<Button> buttons = new List<Button>();

To use it like a two-dimensional array, you will need a nested list (basically, a List that contains a List that contains Button objects). The syntax is a little intimidating, but it's not so hard to figure out what it means:

List<List<Button>> buttons = new List<List<Button>>();

Upvotes: 3

Joan Caron
Joan Caron

Reputation: 1970

you can invoke Clear() Method

http://msdn.microsoft.com/en-us/library/system.array.clear.aspx

Upvotes: -1

Related Questions