简体   繁体   English

如何以特定方法停止代码

[英]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; 只需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. 或者,如果您想停止两个方法的执行,则可以抛出一个异常(如果是这种情况,并且您希望通过异常在代码中处理此类情况),或者让该方法返回一个布尔值,例如一切都很好,返回true,如果出现消息框情况,则返回false,然后在main方法中您知道必须中断。 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: 最简单,最快的方法是从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;
 }

Then act upon that value after calling checkStock() in the SaveAllListItems method: 然后在SaveAllListItems方法中调用checkStock()之后对该值进行操作:

if (!checkStock())  
    return;

This will immediately exit the SaveAllListItems method without executing anymore code in it. 这将立即退出SaveAllListItems方法,而无需在其中执行任何代码。

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. 另外建议:改变的访问checkStock()public要么protectedprivate -这是不可能的,你需要从类/表格外访问此。

return;

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

Should you be using one of the overload functions of MessageBox.Show()? 您是否应该使用MessageBox.Show()的重载功能之一?

for example: public static MessageBoxResult Show( string messageBoxText, string caption, MessageBoxButton button ) 例如:公共静态MessageBoxResult Show(字符串messageBoxText,字符串标题,MessageBoxButton按钮)

So the loop may examine the MessageBoxResult. 因此,循环可以检查MessageBoxResult。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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