invalid expression term 'else'

hi guys me again :slight_smile: with another c# prob :frowning: well basically i’am haveing probs with my “else” part here it is

        string messageText;
        string  PlayerName;
        string firstName;
        firstName = textBox1.Text;
        PlayerName = textBox2.Text;

        
        
        if (firstName == "Chocolate")

            MessageBox.Show("Password accepted");        

            MessageBox.Show(PlayerName);
    
           **else**

           MessageBox.Show("Password not accepted");

(put the stars around the else to make it more visable)(if this is a duplicated question sorry but i coudn’t find one colsely related one so i decided to make my own question)

You solved because you deleted an instruction inside the if statement. When you have just one instruction in the if statement, you can avoid the use of brackets {}. But when you have 2 or more instructions, the compiler has to recognize the correct block and the only way to do that is to enclose the code with "{}", as in your modified code below:

string messageText;
string  PlayerName;
string firstName;
firstName = textBox1.Text;
PlayerName = textBox2.Text;

if (firstName == "Chocolate"){

    MessageBox.Show("Password accepted");        

    MessageBox.Show(PlayerName);

}else

   MessageBox.Show("Password not accepted");