简体   繁体   English

在C#中满足一个IF条件后立即停止执行方法

[英]Stop execution of a method as soon as one IF condition is met in c#

I got a couple of if statements. 我有几个if语句。 What I want is, as soon as one condition is met, I should get out of the method. 我想要的是,一旦满足一个条件,我就应该摆脱这种方法。 I will be using this if statements on filtering data for a datagridview or report. 我将使用if语句过滤datagridview或报表的数据。 With my code below, it doesn't do this. 在下面的代码中,它不会执行此操作。 It seems to go through all conditions. 它似乎经历了所有条件。 As soon as it finds a condition that meets it, the program executes it and proceeds to another if statement and if it sees that it meets the condition also, it too gets executed. 一旦找到满足条件的程序,程序将执行该条件并继续执行另一个if语句,并且如果发现它也满足该条件,它也将被执行。 I think this is pretty basic for you. 我认为这对您来说很基本。 I don't remember or know how I should do this. 我不记得或不知道该怎么做。

private void btnRoute_Click(object sender, EventArgs e)
    {
        if (cbWithRoute.Checked)
        {
            // StartSearch(txtRoute.SelectedValue.ToString());
            MessageBox.Show(@"route");
        }
        if (cbWithRoute.Checked && cbWithWholeSeller.Checked)
        {
            //StartSearch(txtRoute.SelectedValue.ToString(), txtWholeSeller.SelectedValue.ToString());
            MessageBox.Show(@"route wholeseller");
        }
        if (cbWithRoute.Checked && cbWithCustomer.Checked)
        {
            MessageBox.Show(@"route customer");
        } 
        if (cbWithRoute.Checked && cbWithWholeSeller.Checked && cbWithDate.Checked)
        {
            //StartSearch(txtRoute.SelectedValue.ToString(),
            //    txtWholeSeller.SelectedValue.ToString(), Convert.ToDateTime(txtFromDate.Text).ToShortDateString(),
            //    Convert.ToDateTime(txtToDate.Text).ToShortDateString());
            MessageBox.Show(@"route wholseller date");
        }
        if (cbWithRoute.Checked && cbWithCustomer.Checked && cbWithDate.Checked)
        {
            MessageBox.Show(@"route date customer");
        }
        if (cbWithRoute.Checked && cbWithWholeSeller.Checked && cbWithDate.Checked && cbWithCustomer.Checked)
        {
            //StartSearch(txtRoute.SelectedValue.ToString(),
            //    txtWholeSeller.SelectedValue.ToString(), Convert.ToDateTime(txtFromDate.Text).ToShortDateString(),
            //    Convert.ToDateTime(txtToDate.Text).ToShortDateString(), txtCustomer.SelectedValue.ToString());
            MessageBox.Show(@"route wholeseller date customer");
        }
        //else
        //{
        //    MessageBox.Show(@"Check criteria to search.");
        //}

    }

I commented the true code and replaced it with message boxes to show me what checkboxes are checked. 我注释了真实的代码,并用消息框替换了它,以向我显示选中了哪些复选框。 Thank you. 谢谢。

Since your method retrun type is void add a return to end of all the if conditions like below; 因为你的方法retrun类型是voidreturn至年底所有,如果像下面的条件;

if (cbWithRoute.Checked)
{
    // StartSearch(txtRoute.SelectedValue.ToString());
    MessageBox.Show(@"route");
    return;
}
if (cbWithRoute.Checked && cbWithWholeSeller.Checked)
{
    //StartSearch(txtRoute.SelectedValue.ToString(), txtWholeSeller.SelectedValue.ToString());
    MessageBox.Show(@"route wholeseller");
    return;
}

and so on.... 等等....

Where ever you want it to stop executing, place the following line 在您希望它停止执行的任何地方,放置以下行

return;

I wont assume to understand the business logic behind the checks but you may also want to read up on else statements and else if 我不会假设要了解检查背后的业务逻辑,但是您可能还想阅读else语句, else if

Else-if is the concept you need. 否则,如果您需要此概念。 Like below. 像下面。

if (cbWithRoute.Checked)
    {
        // StartSearch(txtRoute.SelectedValue.ToString());
        MessageBox.Show(@"route");
    }
    else if (cbWithRoute.Checked && cbWithWholeSeller.Checked)
    {
        //StartSearch(txtRoute.SelectedValue.ToString(), txtWholeSeller.SelectedValue.ToString());
        MessageBox.Show(@"route wholeseller");
    }

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

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