none
Exit From Function in c#

    Question

  • return; command is used to exit from a void in c#. I know this. But how can i exit from a function in c#? Like in exit funtion command in vb.net. There is an example below:

     private bool Set_Up()
            {
                if (dr_mifare_sistem)
                {
                    listLog.Items.Add("bla bla bla bla");
                          I WANT TO EXIT HERE
                
                }
                cmd = new SqlCommand("SELECT COUNT(*) FROM DEVICES WHERE IP=@IP AND TIP='S'", myconnection);
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@IP", BenimIP);
                if (myconnection.State == ConnectionState.Closed) myconnection.Open();
                if ((int)(cmd.ExecuteScalar()) == 0)
                {.........
    ................................
    ................................

    }
    Thursday, March 12, 2009 1:17 PM

Answers

  • private bool Set_Up() 
            { 
                if (dr_mifare_sistem) 
                { 
                    listLog.Items.Add("bla bla bla bla"); 
                          return false;//////// 
                 
                } 
                cmd = new SqlCommand("SELECT COUNT(*) FROM DEVICES WHERE IP=@IP AND TIP='S'", myconnection); 
                cmd.Parameters.Clear(); 
                cmd.Parameters.AddWithValue("@IP", BenimIP); 
                if (myconnection.State == ConnectionState.Closed) myconnection.Open(); 
                if ((int)(cmd.ExecuteScalar()) == 0) 
                {......... 
    ................................ 
    ................................ 
     


    Thanks, A.m.a.L
    • Marked as answer by Bin-ze Zhao Tuesday, March 17, 2009 6:45 AM
    Thursday, March 12, 2009 1:20 PM
  • You must specify a value of the right kind to return. For a bool function, that's return true or return false (or return a bool variable):

         return true;

    or

         return false;

    or

         bool result = true;
         return result;

    etc.
    • Marked as answer by Bin-ze Zhao Tuesday, March 17, 2009 6:45 AM
    Thursday, March 12, 2009 1:21 PM

All replies

  • private bool Set_Up() 
            { 
                if (dr_mifare_sistem) 
                { 
                    listLog.Items.Add("bla bla bla bla"); 
                          return false;//////// 
                 
                } 
                cmd = new SqlCommand("SELECT COUNT(*) FROM DEVICES WHERE IP=@IP AND TIP='S'", myconnection); 
                cmd.Parameters.Clear(); 
                cmd.Parameters.AddWithValue("@IP", BenimIP); 
                if (myconnection.State == ConnectionState.Closed) myconnection.Open(); 
                if ((int)(cmd.ExecuteScalar()) == 0) 
                {......... 
    ................................ 
    ................................ 
     


    Thanks, A.m.a.L
    • Marked as answer by Bin-ze Zhao Tuesday, March 17, 2009 6:45 AM
    Thursday, March 12, 2009 1:20 PM
  • You must specify a value of the right kind to return. For a bool function, that's return true or return false (or return a bool variable):

         return true;

    or

         return false;

    or

         bool result = true;
         return result;

    etc.
    • Marked as answer by Bin-ze Zhao Tuesday, March 17, 2009 6:45 AM
    Thursday, March 12, 2009 1:21 PM
  • no you didnt understand me correct. I dont want to return any value up there, i want to finish the funtion there and dont want to process rest of the codes below there.
    Thursday, March 12, 2009 1:42 PM
  • Yes we did understand. :)

    As soon as you return something, the function exits.
    Thursday, March 12, 2009 1:50 PM
  • excuse me, normally i write my applications in vb.net and i am not professional in c#. In functions when we return a value, doesnt the compliler run the rest of codes below returned value?
    Thursday, March 12, 2009 1:55 PM
  • No, as soon as it executes a "return" statement, the function is exited and the program continues from the point at which the function was called. Any code after the "return" statement will not be executed.
    Thursday, March 12, 2009 1:57 PM
  • As the method is declared as returning a bool, it must return a bool. This is the same for VB.Net, but old style VB syntax allows you to just "Exit Function" and not expicitly return a value. In that case the default value is returned (False for a Boolean).

    If you want to return something other than true or false, then you'll either need to change the datatype or raise an exception. Looking at your code suggests that the exit at this point is normal processing, so for good practice shouldn't be handled with an exception.
    Thursday, March 12, 2009 2:00 PM
  • Uh oh. Thread concurrecy exception. :-)
    Thursday, March 12, 2009 2:02 PM