Stop second method execution based on if conditions in the first method

early return pattern
multiple return statements
return early pattern php
single entry, single exit
python function early return
return statement in if condition
java multiple return statements
avoiding multiple return statements

I have a method that contains some if statements:

private void ValidateInputs()
        {
            if (txtBox_eventName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please enter a valid event name", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtBox_eventName.Focus();
                return;
            }

            if (nud_noOfGuests.Value < 10 || nud_noOfGuests.Value > 200)
            {
                MessageBox.Show("Please enter no of guests between 10 and 200", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (radBtn_primeRib.Checked == false && radBtn_chickenMarsala.Checked == false && radBtn_gardenLasagna.Checked == false)
            {
                MessageBox.Show("Please make an Entree choice", "Action Reuired", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

I have a second method that does some other stuff. I am calling both of these methods in my click event. I want to stop the program to execute second method if any of the if conditions in the first method is met.

The click event where I am calling both methods is:

private void btn_createEvent_Click(object sender, EventArgs e)
        {
            ValidateInputs();            
            SetValues();

            calcCharges = new CateringEvent(eventName, noOfGuests, selectedEntre, barOption, wineOption);
            lbl_calcEntreCharges.Text = calcCharges.EntreCharge.ToString("C2");
            lbl_calcDrinkCharges.Text = calcCharges.DrinksCharge.ToString("C2");
            lbl_calcSurcharge.Text = calcCharges.Surcharge.ToString("C2");
            lbl_calcTotalCharges.Text = calcCharges.TotalCharge.ToString("C2");

            txtBox_eventName.Enabled = false;
            btn_createEvent.Enabled = false;
            btn_modifyEvent.Enabled = true;
        }

I want SetValues() to run only if none of the if conditions from ValidateInputs() work. How do I achieve that in this case?

You can simply change return type of ValidateInputs method to bool.

private bool ValidateInputs()
{
    if (txtBox_eventName.Text.Trim() == string.Empty)
    {
        MessageBox.Show("Please enter a valid event name", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtBox_eventName.Focus();
        return false;
    }

    if (nud_noOfGuests.Value < 10 || nud_noOfGuests.Value > 200)
    {
        MessageBox.Show("Please enter no of guests between 10 and 200", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }

    if (radBtn_primeRib.Checked == false && radBtn_chickenMarsala.Checked == false && radBtn_gardenLasagna.Checked == false)
    {
        MessageBox.Show("Please make an Entree choice", "Action Reuired", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }
    return true;
}

And then change your click method to:

if (ValidateInputs() == false) // or if(!ValidateInputs())
   return;
SetValues();

Should I return from a function early or use an if statement , I prefer the second style. Get invalid cases out of the way first, either simply exiting or raising exceptions as appropriate, put a blank line in there, then add the  how to stop a method execution if condition is meet Apr 26, 2007 09:29 PM | JohnStern | LINK I have a method that gets a number of records from a database, if the number of records is more that 500 then then method should stop everything and just display a message saying, the result is too large, otherwise it should continue and bind the data

Modify your method to return a Boolean as true or false.

private bool ValidateInputs()
{
    if (txtBox_eventName.Text.Trim() == string.Empty)
    {
        MessageBox.Show("Please enter a valid event name", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtBox_eventName.Focus();
        return false;
    }

    if (nud_noOfGuests.Value < 10 || nud_noOfGuests.Value > 200)
    {
        MessageBox.Show("Please enter no of guests between 10 and 200", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }

    if (radBtn_primeRib.Checked == false && radBtn_chickenMarsala.Checked == false && radBtn_gardenLasagna.Checked == false)
    {
        MessageBox.Show("Please make an Entree choice", "Action Reuired",MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }
    return true;
}

Then inside your click event check the first method's output.

if(ValidateInputs()){
        secondmethod();
}

Bioinspired Optimization Methods and Their Applications: 8th , The base fitness is the number of errors divided by total number of symbols. If Since errors early on in the input words ω0 ,,ω n will cause errors later, a word ωi, is only PMIT uses three termination conditions to determine when to stop running. Second, PMIT stops after 4 h of execution if no solution has been found. Relaxation to this condition is available to export oriented units; products manufactured by MSME units wherein banks may apply the first method. Turnover method (Nayak Committee norms) Under turnover method, the aggregate fund-based working capital limits are computed on the basis of Minimum of 20% of their projected annual turnover. The

The simplest way for the code you posted is to have your ValidateInputs return a bool, that is, a boolean (true or false). Then in your btn_createEvent_Click method check for the returned value of ValidateInputs with an if conditional and decide to or not to call SetValues. The corrected code is as follows:

private bool ValidateInputs()
{
    if (txtBox_eventName.Text.Trim() == string.Empty)
    {
        MessageBox.Show("Please enter a valid event name", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtBox_eventName.Focus();
        return false;
    }

    if (nud_noOfGuests.Value < 10 || nud_noOfGuests.Value > 200)
    {
        MessageBox.Show("Please enter no of guests between 10 and 200", "Action Required", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }

    if (radBtn_primeRib.Checked == false && radBtn_chickenMarsala.Checked == false && radBtn_gardenLasagna.Checked == false)
    {
        MessageBox.Show("Please make an Entree choice", "Action Reuired",MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
    }
    return true;
}

private void btn_createEvent_Click(object object sender, EventArgs e)
{

    if(ValidateInputs()){
        secondmethod();
    }

    calcCharges = new CateringEvent(eventName, noOfGuests, selectedEntre, barOption, wineOption);
    lbl_calcEntreCharges.Text = calcCharges.EntreCharge.ToString("C2");
    lbl_calcDrinkCharges.Text = calcCharges.DrinksCharge.ToString("C2");
    lbl_calcSurcharge.Text = calcCharges.Surcharge.ToString("C2");
    lbl_calcTotalCharges.Text = calcCharges.TotalCharge.ToString("C2");

    txtBox_eventName.Enabled = false;
    btn_createEvent.Enabled = false;
    btn_modifyEvent.Enabled = true;
}

Proceedings, the Second International Conference on Industrial & , Cost-Based Function Method for Determining Number of Iterations The (T) to be the highest and the lowest value obtained so far during the execution of the system. modified so that it would stop if any one of the following conditions was true. As a result, the first two conditions remained as the stopping criterion except  To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program running. It is the most reliable, cross-platform way of stopping code execution. Here is a simple example.

Fortran 77 and Numerical Methods, 'FIRST ROOT =', F10.3 // IX, 'SECOND ROOT =', F10.3) STOP END 3.4 Logical IF statement The logical if condition checks any given logical condition and  Overview. The primary means of execution in the U.S. have been hanging, electrocution, the gas chamber, firing squad, and lethal injection. The Supreme Court has never found a method of execution to be unconstitutional, though some methods have been declared unconstitutional by state courts.

MotorBoating, As a matter of fact, you don't need it hoisted all the way: all you need—if you For example, the correct method of docking at the weather side of a wharf in and second, you'll have less speed to dissipate if and when you want to stop. ( Note: obviously, steps can be omitted, depending on conditions and circumstances.)  Decision Making in programming is similar to decision making in real life. In programming also we face some situations where we want a certain block of code to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions.

Welding Engineer, The first type of automatic spot welder consists of machines in which the pressure reaches a predetermined value the circuit is broken and the welding stops. The second method, if property executed, will produce welds much less brittle, there arc many special features employed to obtain welds where conditions are  I know that Thread.Sleep() method takes an argument and I had used Thread.Sleep(5000). But I really don't know why it is not working. As I said the code is in the for loop. Initially thread.sleep(5000) responds accurately but after 10 records it executes the code very fast (even less than a second). Why this is happening I don't know.

Comments
  • Please show method which you call on click event. My guess is to not return void, but bool, then you could decide if second method should be called.
  • @Hostel Updated the description with click method.
  • ValidateInputs should return bool, then in click method you will put if statement. If ValidateInpute == false then you should return and not call the rest.