none
how to use the break statement in c# ?

    Question

  • Hi all

    i am having an error in running this code ,please help

    public bool AddSubFunction(string a)
            {
                if (a == "ok")
                {
                    
                    MessageBox.Show("Return false");
                    return false;
                    break;   
                }
                                
                else
                   
                    MessageBox.Show("Retrun True");
                return true;
                
            }

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                string ab = textBox1.Text;

                if (AddSubFunction(ab))
                {
                    MessageBox.Show("Return true inside main");
                }
                else
                    MessageBox.Show("Return false inside main");
            }


    MSDN Forum

    Wednesday, January 16, 2013 12:24 PM

Answers

  • Hi,

    It will give an error for break

      if (a == "ok")
                {
                    
                    MessageBox.Show("Return false");
                    return false;
                    break;   
                }

    Since there is no loop from which it will break.

    Use of break must be within any loop(for-loops, foreach-loops and switch-statements).

    What is the purpose of using break over there?

    return is sufficient to get result.

    • Proposed as answer by Blackwood Wednesday, January 16, 2013 1:59 PM
    • Marked as answer by Amal E S Wednesday, January 16, 2013 2:52 PM
    Wednesday, January 16, 2013 12:35 PM
  • VB.Net includes Exit Function only because it wants to support (in addition to the Return statement) the old VB way of returning values from a function like this

    Function IsPositive(parm as Integer) As Boolean
        If parm > 0 then 
            IsPositive = True
            Exit Function
        Else
            IsPositive = False
            Exit Function
        End If
    End Function

    I don't think C# supports this legacy style of coding.  In your C# code, you use the "return" statement to both set the return value and exit the function (as you also could in VB.Net). There is no need for a C# equivalent to Exit Function as you should not exit a function without setting a return value.


    • Edited by Blackwood Wednesday, January 16, 2013 3:39 PM
    • Marked as answer by Amal E S Thursday, January 17, 2013 1:24 AM
    Wednesday, January 16, 2013 3:35 PM
  • Yes. The Return statement is the normal way to exit a function in VB.Net. As I said, Exit Function is only their so that you can still code the old style as in the example in my earlier post. There is no advantage in the old style, and indeed it makes it easy for you to accidentally exit the function without setting the return value.

    I don't think allowing the use of this old style of coding makes VB.Net more powerful than C#, just more forgiving. I'm sure some people would say that VB.Net's continued support of these old styles of coding is a disadvantage rather than an advantage.

    • Marked as answer by Amal E S Thursday, January 17, 2013 3:49 AM
    Thursday, January 17, 2013 2:09 AM
  • Actually in Vb.Net we can use "Exit function"  to exit from a function .  So i only found an alternative keyword in C# as break.

    but now i know that it is not applicable over here.

    So can you just explain an alternative to quit from a function if my condition is true.


    MSDN Forum

    • Marked as answer by Amal E S Wednesday, January 16, 2013 2:52 PM
    Wednesday, January 16, 2013 2:51 PM

All replies

  • Hi,

    It will give an error for break

      if (a == "ok")
                {
                    
                    MessageBox.Show("Return false");
                    return false;
                    break;   
                }

    Since there is no loop from which it will break.

    Use of break must be within any loop(for-loops, foreach-loops and switch-statements).

    What is the purpose of using break over there?

    return is sufficient to get result.

    • Proposed as answer by Blackwood Wednesday, January 16, 2013 1:59 PM
    • Marked as answer by Amal E S Wednesday, January 16, 2013 2:52 PM
    Wednesday, January 16, 2013 12:35 PM
  • Hi,
    here you can find good explanation of break statement.
    And in your code you should remove break statement as it does not follow the rule to use it.

    public bool AddSubFunction(string a)
             {
                 if (a == "ok")
                 {
                     
                     MessageBox.Show("Return false");
                     return false;
                     //break;   
                 }
                                 
                 else
    		{
                    
                     MessageBox.Show("Retrun True");
                     return true;
    		}
                 
             } 
    
    
     private void button1_Click(object sender, RoutedEventArgs e)
             {
                 string ab = textBox1.Text;
     
                if (AddSubFunction(ab))
                 {
                     MessageBox.Show("Return true inside main");
                 }
                 else
                     MessageBox.Show("Return false inside main");
             } 

    Let me know if your problem persists.


    One good question is equivalent to ten best answers.



    Wednesday, January 16, 2013 12:50 PM
  • Actually in Vb.Net we can use "Exit function"  to exit from a function .  So i only found an alternative keyword in C# as break.

    but now i know that it is not applicable over here.

    So can you just explain an alternative to quit from a function if my condition is true.


    MSDN Forum

    • Marked as answer by Amal E S Wednesday, January 16, 2013 2:52 PM
    Wednesday, January 16, 2013 2:51 PM
  • Actually in Vb.Net we can use "Exit function"  to exit from a function .  So i only found an alternative keyword in C# as break.

    but now i know that it is not applicable over here.

    So can you just explain an alternative to quit from a function if my condition is true.


    MSDN Forum

    • Marked as answer by Amal E S Wednesday, January 16, 2013 2:52 PM
    • Unmarked as answer by Amal E S Wednesday, January 16, 2013 2:52 PM
    Wednesday, January 16, 2013 2:51 PM
  • VB.Net includes Exit Function only because it wants to support (in addition to the Return statement) the old VB way of returning values from a function like this

    Function IsPositive(parm as Integer) As Boolean
        If parm > 0 then 
            IsPositive = True
            Exit Function
        Else
            IsPositive = False
            Exit Function
        End If
    End Function

    I don't think C# supports this legacy style of coding.  In your C# code, you use the "return" statement to both set the return value and exit the function (as you also could in VB.Net). There is no need for a C# equivalent to Exit Function as you should not exit a function without setting a return value.


    • Edited by Blackwood Wednesday, January 16, 2013 3:39 PM
    • Marked as answer by Amal E S Thursday, January 17, 2013 1:24 AM
    Wednesday, January 16, 2013 3:35 PM
  • Hi Blackwood

    VB.Net also have Return keyword.

    I think Vb.net is more powerful and flexible than C#.

    Thanks for your valuable answer.


    MSDN Forum

    Thursday, January 17, 2013 1:24 AM
  • Yes. The Return statement is the normal way to exit a function in VB.Net. As I said, Exit Function is only their so that you can still code the old style as in the example in my earlier post. There is no advantage in the old style, and indeed it makes it easy for you to accidentally exit the function without setting the return value.

    I don't think allowing the use of this old style of coding makes VB.Net more powerful than C#, just more forgiving. I'm sure some people would say that VB.Net's continued support of these old styles of coding is a disadvantage rather than an advantage.

    • Marked as answer by Amal E S Thursday, January 17, 2013 3:49 AM
    Thursday, January 17, 2013 2:09 AM