How do you put that in a for cycle?

Asked

Viewed 71 times

0

code:

if (label1.Text != string.Empty && label2.Text != string.Empty && label3.Text != string.Empty && label4.Text != string.Empty && label5.Text != string.Empty && label6.Text != string.Empty && label7.Text != string.Empty && label8.Text != string.Empty && label9.Text != string.Empty)
{
    empate();
    return true;
}
  • the comparison is done wrong ... and if you have a sequence can be made a cycle really picking up the label for some question, by the controls of this form ... I think it’s Windows Forms or it’s Webforms?

  • It’s windows Forms. Comparison made wrong?

1 answer

5

It is not very clear what you want, but if you want to replace this long line with a cycle for, can do so:

int count = 0;
for (int i = 1; i <= 9; i++)
{
    if (this.Controls["label" + i].Text != string.Empty) count++;
}

if (count == 9)
{
    empate();
    return true;
}

Another option would be to use a container, for example a panel, with 9 labels inside, and do:

if (this.panel1.Controls.Cast<Label>().Count(lbl => lbl.Text != string.Empty) == 9)
{
    empate();
    return true;
}
  • That’s exactly it, thank you

  • @Gonçalosousa edited the answer with another option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.