locked
Why are braces used differently in these 2 examples RRS feed

  • Question

  • 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

    Monday, January 13, 2014 4:20 PM

Answers

  • Hello jt305,

    Can you please paste your modified code, then it will be easy to resolve your issue ?


    Regards, http://www.shwetalodha.blogspot.in/

    • Marked as answer by jt305 Monday, January 13, 2014 8:15 PM
    Monday, January 13, 2014 4:54 PM
  • Hello,

    When you have multiple statements and you want them to be executed conditionally you need to wrap them in bracers, for a single statement you don't need to add any bracer at all, however, I'd put bracers in both cases because in my opinion it makes the code more readable and consistent.

    So in both cases you can opt to remove the bracers.


    Regards,

    Eyal Shilony

    • Edited by Eyal Solnik Monday, January 13, 2014 5:07 PM
    • Proposed as answer by Carlos Silva UK Monday, January 13, 2014 5:57 PM
    • Marked as answer by jt305 Monday, January 13, 2014 8:15 PM
    Monday, January 13, 2014 5:03 PM
  • Thank You.

    I must have done something wrong the first time.

    I removed the second set of braces and it worked fine.

    I will take your advice though, by always using the braces to make the code easier to understand.

    Also, Thanaks to Shweta for your response.

    Regard,

    Jim

    • Marked as answer by jt305 Monday, January 13, 2014 8:15 PM
    Monday, January 13, 2014 8:14 PM

All replies

  • Hello jt305,

    Can you please paste your modified code, then it will be easy to resolve your issue ?


    Regards, http://www.shwetalodha.blogspot.in/

    • Marked as answer by jt305 Monday, January 13, 2014 8:15 PM
    Monday, January 13, 2014 4:54 PM
  • Hello,

    When you have multiple statements and you want them to be executed conditionally you need to wrap them in bracers, for a single statement you don't need to add any bracer at all, however, I'd put bracers in both cases because in my opinion it makes the code more readable and consistent.

    So in both cases you can opt to remove the bracers.


    Regards,

    Eyal Shilony

    • Edited by Eyal Solnik Monday, January 13, 2014 5:07 PM
    • Proposed as answer by Carlos Silva UK Monday, January 13, 2014 5:57 PM
    • Marked as answer by jt305 Monday, January 13, 2014 8:15 PM
    Monday, January 13, 2014 5:03 PM
  • Thank You.

    I must have done something wrong the first time.

    I removed the second set of braces and it worked fine.

    I will take your advice though, by always using the braces to make the code easier to understand.

    Also, Thanaks to Shweta for your response.

    Regard,

    Jim

    • Marked as answer by jt305 Monday, January 13, 2014 8:15 PM
    Monday, January 13, 2014 8:14 PM
  • You welcome. :)

    Regards,

    Eyal Shilony

    Monday, January 13, 2014 9:53 PM