C# 构建复杂的If语句

C# 构建复杂的If语句,c#,winforms,C#,Winforms,我有三个if语句,它们显然在处理不同的函数。我想将它们组合成一个函数,因此我必须组合if语句。但是我被困在了如何使用|&和()的问题上 我的功能作为过滤器,用户可以填写任何文本框。在按钮单击事件中,代码将查找满足条件的代码。其中三个独立工作,但将它们结合起来非常困难。请容忍我和帮助我,我只是一个非常新的程序员,没有任何背景。我被困了好几天;( 我的过滤器快照: 第一: if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLowe

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

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

我的过滤器快照:

第一:

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 ( (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)
bool condition1=!DateTime.TryParseExact(txtComStartDate.Text,“dd/MM/yy”,提供程序,datetimestyle.AssumeLocal,out startDate);
bool condition2=DateTime.Parse(itemDate,provider,DateTimeStyles.AssumeLocal)>=startDate);
bool条件3=!DateTime.TryParseExact(txtComEndDate.Text,“dd/MM/yy”,提供程序,DateTimeStyles.AssumeLocal,out-endDate);

bool condition4=DateTime.Parse(itemDate,provider,DateTimeStyles.AssumeLocal)据我所知,您希望同时对所有3个进行求值,但简单地将它们添加到一个大行中将很难读取或维护。我建议为您以前的每个ifs设置单独的bool值:

bool firstIf = (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower());
然后在一个语句中比较所有3个:

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

这样,以后在需要时更容易进行更改,而且您仍然可以阅读代码。

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

如果((A&B)|(C&D))
将计算
(A&B)
(C&D)
段,则“或”这些中间结果一起产生最终值


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

如果您将这些子句中的每一个分解为函数并相应地划分复杂性,这将有助于您理解。较小的部件更易于使用,并且从长远来看更易于维护。当您作为一名程序员发展时,您最终将根本不使用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;
}
public void btnAnalyze_onClick(){
列表结果=新建ArrayList();
if(txtComAuthor.Text!=String.Empty)
{ 
列表匹配=filterByAuthor(txtComAuthor.Text);
结果:addRange(匹配);
}
if(txtComStartDate.Text!=String.Empty)
{
列表匹配=filterByStartDate(txtComStartDate.Text);
结果:addRange(匹配);
}
//对其他人也这样做
返回结果;
}
公共列表筛选器权限(字符串desiredAuthorName){
列表匹配项=新的ArrayList();
//这里有您的数据访问块,来自DB/Excel/任何东西。
列出候选人=。。。
foreach(候选项中的候选项){
如果(candidate.ToLower()==desiredAuthorName){
匹配项。添加(候选项)
}
}
返回比赛;
}
有经验的程序员会意识到这里有很多重复,并且会对违反DRY和性能的行为感到不满。没关系。它可以重构。对于新手来说,这将是最容易理解的风格

如果您遵循这种风格,那么在需要进行过滤的地方应该很明显。基本上,您需要将foreach循环中的if语句替换为所考虑的文本字段的条件


但是你不需要把一堆子句放在一起,因为你把事情分解得更好了。如果您仍然需要一些嵌套的If,请将其进一步分解为更小的函数。

在我进一步工作之前,是否有理由组合If语句?如果您将它们放在单独的行中,它们可能更易于维护。在您新的
if
语句中,您是否尝试测试第一、第二和第三个条件是否都
true
,或者是否只有一个
true
?@Doc我不知道:(因为我只有一个按钮可以点击,点击事件会搜索我所有的XML文件,然后返回符合条件的文件(例如作者、相关日期、关键字或作者姓名+关键字1+开始日期的组合)@DFord如果其中任何一个都填写了,那么结果将只显示符合我向Doc解释的标准的人。听起来很疯狂:(@Shyuan听起来你不想把它们合并成一个if语句。我听起来你想保留每个if语句,如果每个if语句的计算结果都为true,那么做一些事情,比如使用该文本框进行搜索。对吗?我确实相信我是忍者d。但是我会有很多条件,最后if语句会非常长嗯,我是对的还是错的?:|@Shyuan,事实上,不是。如果语句实际上只是布尔比较(寻找真或假的语句)在这种情况下,您只需保存真值和假值以备将来使用,然后比较结果。这在语义上是正确的,但以后会成为维护的噩梦。每种情况都是不同类型的过滤。如果我们试图表现得优雅,我们会建议使用LINQ,但现在这会带来太多开销。而不是鼓励共谋ed表达式逻辑内联,我将进一步分解。
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;
}