简体   繁体   中英

How to stop code in specific method

I have two method in which one method check stock and other method calls series of other method.

Check Stock Method.

public void checkStock()
{
    foreach (var listBoxItem in listBox1.Items)
    {
        if (Convert.ToInt32(GetStock(listBoxItem.ToString())) == 0)
        {
            MessageBox.Show(listBoxItem.ToString() + " not in Stock!. Please delete the item before proceeding");

        }
     }
 }

Another method is

private void SaveAllListItems()
{

    string listItems = string.Empty;
    foreach (var listBoxItem in listBox1.Items)
    {

            listItems += listBoxItem.ToString();

            if (listBox1.Items.IndexOf(listBoxItem) < listBox1.Items.Count - 1)
            {
                listItems += ", ";
            }
    }

    checkStock();

    UpdateStock();

    InsertUser(maskedTextBox1.Text, comboBox1.Text, maskedTextBox2.Text, maskedTextBox3.Text, maskedTextBox4.Text, maskedTextBox5.Text,
                  maskedTextBox6.Text, maskedTextBox7.Text, maskedTextBox8.Text, maskedTextBox9.Text);

    InsertOrder(Convert.ToInt32(GetID(maskedTextBox1.Text)), orderNumber(), listItems, DateTime.Now, maskedTextBox10.Text, get_next_id());        
}

I want to stop code execution if the messagebox is displayed by first method.

What is the quick fix?

Just do a return; on the condition where you have to break the execution and that will exit the method that is currently running.

Or, if you want to stop both methods from executing then you either throw an exception (if that is the case, and you want to handle cases like this in your code via exceptions) or have the method return a boolean value for instance and if all is good return true, if the messagebox situation appears return false and then in the main method you know you have to break. Like so:

   if (!checkStock()) { return; }

switch the return type of checkstock

public bool checkStock()
{
     foreach (var listBoxItem in listBox1.Items)
     {
         if (Convert.ToInt32(GetStock(listBoxItem.ToString())) == 0)
         {
              MessageBox.Show(listBoxItem.ToString() + " not in Stock!. Please delete the item before proceeding");
              return false;
         }
     }
return true;
}

and in your main code do something like this:

if (!checkStock())
{
     //EXIT
     return;
}

The simplest and quickest way is to return a boolean value from the checkStock() method:

public bool checkStock()
{
    foreach (var listBoxItem in listBox1.Items)
    {
        if (Convert.ToInt32(GetStock(listBoxItem.ToString())) == 0)
        {
            MessageBox.Show(listBoxItem.ToString() + " not in Stock!. Please delete the item before proceeding");
            return false;
        }
     }

    return true;
 }

Then act upon that value after calling checkStock() in the SaveAllListItems method:

if (!checkStock())  
    return;

This will immediately exit the SaveAllListItems method without executing anymore code in it.

Further suggestion: change the accessor of checkStock() from public to either protected or private - it is unlikely that you need to access this from outside the class/form.

return;

将立即停止并返回,而无需执行其余方法。

Should you be using one of the overload functions of MessageBox.Show()?

for example: public static MessageBoxResult Show( string messageBoxText, string caption, MessageBoxButton button )

So the loop may examine the MessageBoxResult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM