What’s wrong with parole?

Asked

Viewed 113 times

-4

What the error in the parole below and how to resolve?

private void Botão_Click(object sender, EventArgs e)
{
    if(Pbox.Show();)
        Pbox.Hide();
    else(Pbox.Hide();)
        Pbox.Show();
}

private void Pbox_Click(object sender, EventArgs e)
{

}
  • 5

    What a mess of code. else has no associated condition, in condition if can’t have ;. Then there’s the problem of what you want to do that feels wrong. But before you see what you want to do you should learn the syntax of the language.

1 answer

8


The syntax is quite wrong. Anyway there is an easier way to do what you want:

private void Botão_Click(object sender, EventArgs e) {
    PBox.Visible = !Pbox.Visible;
}

I put in the Github for future reference.

If I were to write in the form of the question it should be so:

if (Pbox.Visible) {
    Pbox.Hide();
} else {
    Pbox.Show();
}

The methods Hide() and Show() do not return anything so cannot be used as a condition of if that expects a boolean. To know if the control is visible use the property Visible. It is not necessary to use a else since this property can only return two valore, or is true, or is false, if true executes the block of the if, otherwise it will execute the else, independent of anything else. Even if it were the case to use a condition, it could only with a else if.

  • I didn’t even know that C# accepted Unicode for function names (and maybe classes): Botão_Click

  • 1

    @Related Guilhermenascimento: https://answall.com/q/16555/101

Browser other questions tagged

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