What is the error in the conditional? [closed]

-4

What is the error in the conditional below and how to solve it?

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)
{

}
    
asked by anonymous 25.05.2017 / 10:30

1 answer

9

The syntax is wrong. Either way it has an easier way to do what you want:

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

I've placed it on GitHub for future reference.

If it were to write the question form it should look like this:

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

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

    
25.05.2017 / 12:25