构建复杂的If语句

时间:2012-08-20 16:27:19

标签: c# winforms

我有三个if语句,他们显然正在处理不同的功能。我想将它们组合成一个函数,因此我必须结合if语句。但我被困在如何使用|| &&()

我的过滤器功能,用户可以填写任何文本框。在按钮单击事件上,代码将找到符合条件的代码。其中三个独立工作,但结合起来非常困难。请耐心帮助我,我只是一个非常新的程序员,根本没有背景。我被困了几天。 ;(

我的过滤器快照:

Filters

首先:

if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower())

第二

if ((!DateTime.TryParseExact(txtComStartDate.Text, "dd/MM/yy", provider, DateTimeStyles.AssumeLocal, out startDate)
      || DateTime.Parse(itemDate, provider, DateTimeStyles.AssumeLocal) >= startDate) &&
      (!DateTime.TryParseExact(txtComEndDate.Text, "dd/MM/yy", provider, DateTimeStyles.AssumeLocal, out endDate)
      || DateTime.Parse(itemDate, provider, DateTimeStyles.AssumeLocal) <= endDate))

第三

if (txtComKeyword1.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
    txtComKeyword2.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword2.Text.ToLower()) ||
    txtComKeyword3.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword3.Text.ToLower()) ||
    txtComKeyword4.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword4.Text.ToLower()))

4 个答案:

答案 0 :(得分:4)

是否使用||或者&amp;&amp;取决于你是否想要含义至少有一个条件为真(使用||)或所有条件必须为真(使用&amp;&amp;)。

如果您需要混合两种含义,请使用()来相互评估条件,例如

if ( (a && b) || (c && d))

表示 a和b都为真 c和d都为真

如果为复合逻辑的每个部分定义单独的布尔值,则可以更容易地读取和维护代码。没有性能差异。

bool condition1 = !DateTime.TryParseExact(txtComStartDate.Text, "dd/MM/yy", provider, DateTimeStyles.AssumeLocal, out startDate);
bool condition2 = DateTime.Parse(itemDate, provider, DateTimeStyles.AssumeLocal) >= startDate);
bool condition3 = !DateTime.TryParseExact(txtComEndDate.Text, "dd/MM/yy", provider, DateTimeStyles.AssumeLocal, out endDate);
bool condition4 = DateTime.Parse(itemDate, provider, DateTimeStyles.AssumeLocal) <= endDate);

if ((condition1
      || condition2 &&
      (condition3
      || condition4)

答案 1 :(得分:2)

如果您将这些条款中的每一个细分为函数并相应地分解复杂性,它将帮助您理解。较小的部件更易于使用,并且从长远来看更易于维护。当你作为程序员进化时,你最终根本不会使用if语句,而是利用多态的力量。

现在,首先将事情分开。

public void btnAnalyze_onClick(){
    List<Item> results = new ArrayList<Item>();
    if(txtComAuthor.Text != String.Empty)
    { 
       List<Item> matched = filterByAuthor(txtComAuthor.Text);
       results.addRange(matched);
    }
    if(txtComStartDate.Text != String.Empty)
    {
       List<Item> matched = filterByStartDate(txtComStartDate.Text);
       results.addRange(matched);
    }
    // do the same for the others
    return results;
}



public List<Item> filterByAuthor(String desiredAuthorName){
      List<Item> matches = new ArrayList<Item>();
      //have your data access piece here, from DB/Excel/whatever.
      List<Item> candidates = ...
      foreach(Item candidate in candidates){
         if(candidate.ToLower() == desiredAuthorName){ 
            matches.add(candidate)
         }
       }
       return matches;
}

经验丰富的程序员会意识到这里有很多重复,并且会适应违反DRY和性能的行为。没关系。它可以重构。对于新手来说,这将是最容易理解的风格。

如果您遵循这种风格,那么您应该明白需要进行过滤的位置。基本上,您需要使用您正在考虑的文本字段的条件替换foreach循环中的if语句。

但你不应该一起添加一堆子句,因为你已经把事情分开了一点点。如果您仍然发现需要一些嵌套ifs,请将其细分为更小的函数。

答案 2 :(得分:1)

如果对逻辑分组有疑问,请在每对操作周围加上括号。这样你就知道如何组合这对。

if ((A && B) || (C && D))将评估(A && B)(C && D)段,然后将“或”这些中间结果一起评估,以产生最终值。

进一步阅读,搜索布尔逻辑的交换,关联和分配属性。

答案 3 :(得分:0)

据我所知,您希望同时评估所有3个,但只是将它们添加到一个大行中将难以阅读或维护。我建议为以前的每个ifs设置单独的bool值:

bool firstIf = (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower());

然后将所有3个语句进行比较:

if (firstIf && secondIf && thirdif)
{
    Console.WriteLine("It works!");
}

这样,如果需要,以后更容易进行更改,您仍然可以阅读代码。

相关问题