[Solved] } Expected in Routine


Learning how to format code is a CRITICAL skill for writing code that works correctly.

private void PathExist()
{
    if (Directory.Exists(folderBrowserDialog.SelectedPath + @"\"))
    {
        lblExist.Visible = false;
        Properties.Settings.Default.FirstTime = false;
        // Create a new instance of the Form2 class
        MainFrm MainForm = new MainFrm();
        MainForm.Show();
        Hide(); //This is where Visual Studio is telling me that a "}" is expected
    }        
    else 
    {
        lblExist.Visible = true;
        lblExist.Text = "Folder doesn't exist";
        lblExist.ForeColor = Color.Red;

    }
}

Note how an opening { has been added immediately after the if statement, with a corresponding closing } right before the else.

Now, you might claim “but I’ve seen code in C# where the braces appear optional!” That is only applicable to single logical lines like the following:

if (condition)
    ExecuteMethod();
else 
    ExecuteOtherMethod();

If any block of code within a condition is more than one “instruction”, you must wrap that code in enclosing braces. In addition, some C# developers will insist that ALL code should ALWAYS be wrapped in braces to avoid confusion and as a matter of good practice.

2

solved } Expected in Routine