0

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?

3
  • What do you mean clear and reset? Commented Mar 25, 2013 at 22:19
  • In the same way you clear an array.. maybe I'm trying to do somethings that doesn't exist Commented Mar 25, 2013 at 22:20
  • buttons is an array. You can just set the element to null Commented Mar 25, 2013 at 22:21

2 Answers 2

3

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>>();
Sign up to request clarification or add additional context in comments.

2 Comments

@Hogan You can declare a List<List<Button>> which operates pretty much the same.
@cody how do I populate the List<List<>>
-1

you can invoke Clear() Method

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.