Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Why are braces used differently in these 2 examples

$
0
0

On the C# tutorial for the PictureViewer, there are differences in the way the [if] statement uses the braces for 2 similar buttons.

Here is the 2 variations of code:

First for the show.button.......................................

private void showButton_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(openFileDialog1.FileName);
}
}
**********************************************
Second for the backgroundbutton........................................

private void backgroundButton_Click(object sender, EventArgs e)
{
// Show the color dialog box. If the user clicks OK, change the
// PictureBox control's background to the color the user chose.
if (colorDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.BackColor = colorDialog1.Color;
}

*************************************************

My question: Why does the 'showbutton' have a second set of braces that contains the statement to execute,

when the 'backgroundbutton' has the statement to execute directly under the 'if' and between the starting and ending braces.

I tried to change to fist set of braces and got an error.

Please clarify if you can. Thanks, jt305


Viewing all articles
Browse latest Browse all 31927

Trending Articles