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.
accessButtons()method cannot access those very same buttons"?buttonsisstatic? Do you want multiple instances of the class to manipulate the same collection?accessButtonsshould be changing every buttons text to "this wont work" but it does not. And no,buttonsshould not have beenstatic. It seems I forgot to revert it back after one of my trialsstaticmay be enough to resolve the issue, then.