Özenç B.
Özenç B.

Reputation: 1038

Cannot access array of buttons within the same class

So, I have an ASPX file with 9 buttons (Working on a TicTacToe game so each button has the same OnClick method) and on C#, I find and store all these buttons in an array like so:

public partial class Index : System.Web.UI.Page {
    protected static Button[] buttons;

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            initialConditions();
        }
    }

    private void initialConditions() {
        buttons = findButtons();

        // Can access and modify buttons here
        foreach (var btn in buttons) {
            btn.Text = "this does work";
        }

        // Cant modify buttons here
        accessButtons();
    }

    private Button[] findButtons() {
        List<Button> tmpButtons = new List<Button>();
        foreach (var item in board.Controls) {
            if (item is Button) {
                tmpButtons.Add((Button)item);
            }
        }
        return tmpButtons.ToArray();
    }

    private void accessButtons() {
        foreach (var button in buttons) {
            button.Text = "this wont work";
        }
    }
}

I can modify buttons after initialising the array in initialCondition() method but accessButtons() method cannot access those very same buttons. I guess it has something to do with reference and instance stuff but I cannot get my head around this. How can I access and modify this array of buttons anywhere within the class easily with for loops and such?

Edit: I tried initialising the array like protected static Button[] buttons = findButtons(); but when I do so, I have to make accessbuttons() a static method but then it cannot access board element on ASPX so I could not figure that one out.

Upvotes: 0

Views: 117

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416081

Change this:

protected static Button[] buttons;

To this:

protected List<Button> buttons;

And this:

private Button[] findButtons() {
    List<Button> tmpButtons = new List<Button>();
    foreach (var item in board.Controls) {
        if (item is Button) {
            tmpButtons.Add((Button)item);
        }
    }
    return tmpButtons.ToArray();
}

To this:

private List<Button> findButtons() {
    return board.Controls.OfType<Button>().ToList();
}

And finally this:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        initialConditions();
    }
}

To this:

protected void Page_PreInit(object sender, EventArgs e) {
    initialConditions();
}

Notice that every one of these changes results in less code than you had before.

For this last change, remember that every server event (including button clicks) is a complete round-trip between the web server and the browser, with a completely different instance of the page class. You have to rebuild that array on every run through the page life cycle, and if you want ViewState to work you need to do it before Page_Load().

Upvotes: 2

sommmen
sommmen

Reputation: 7658

What if you change this:

   private void accessButtons() {
        foreach (var button in buttons) {
            button.Text = "this wont work";
        }
    }

To this:

   private void accessButtons() {
        foreach (var button in findButtons()) {
            button.Text = "this wont work";
        }
    }

Because this;

protected static Button[] buttons;

is protected static

So you can only access it like so;

var buttons = Index.buttons;

but it will be null or empty since its not filled anywhere.

Upvotes: 0

Related Questions