Curley Crazy {

What is the difference here: (they both work)

{

for (int i = 0; i < listBox1.Items.Count; i++)

{

MessageBox.Show(listBox1.ItemsIdea.ToString());

}

AND/....................

{

for (int i = 0; i < listBox1.Items.Count; i++)

MessageBox.Show(listBox1.ItemsIdea.ToString());

{

}

 

}

}

between curlies and not....this could drive me insane.

Thanks!




Answer this question

Curley Crazy {

  • XIU

    If you are transitioning from VB.NET, you can think of the curly brackets as start and end of blocks. 

    For example, in VB.NET


    If val1 > val2 Then
       'do something
       'do something else
    End If

     


    Would be written as:

    if (val1 > val2)
    {
       //do something
       //do something else
    }

     


    Notice the curly brackets should always be written after a statement.  The final curly bracket in a block would go in the same place you'd but an End If, End, Next, Wend, etc. in VB.NET.

    Hope this helps,
    Josh Lindenmuth



  • abarone

    Hi,

    you will notice the difference if you for Example execute 2 methods in your for loop.

    for (...)
    {
       method1();
       method2();
    }

    both methods will be executet in the for-loop.

    for (...)
       method1();
       method2();

    only method1 will be executet in the for-loop. method2 will be executed after the loop.

    Thats the trick ;)



  • Hibri

    ahhh... Thanks.


    so the } is like and exit for (VB)...

  • RyanK

    Thanks guys.

    I just went thru the 8 hours of Microsoft Absolute Newbie videos.

    They really did cover mucho , mucho...

    However...when a new concept was introduced...loops, if etc....the code was always pasted. Now I know why.

    Nobody could do it live (lol)...and Im sure Microsoft wanted to help end the well known brace crazies. It won't happen.

    Thanks again.

  • iskander.sierra

    woops....where is example 2 (within what brackets )

  • Curley Crazy {