如果功能在一种情况下完美无缺 [英] if function works flawless in one case

查看:87
本文介绍了如果功能在一种情况下完美无缺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作哈希比较实用程序,我使用if语句将生成的哈希值与文本文件中保存的哈希列表进行比较。除了没有在列表视图中添加假作为子项目,空白暗示错误,项目不匹配,这是我想要避免的。

我工作完美无瑕。

完美无瑕

making hash compare utility and I am using an if statement to compare a generated hash to a list of hashes held in a text file. I works flawlessly with the exception of not adding "false" to the listview as a subitem, a blank " " implies false, that the items do not match, which is what I am trying to avoid.

Flawless

private void compareToolStripMenuItem_Click(object sender, EventArgs e)
       {
           try
           {
               foreach (string hashEntries in File.ReadAllLines(dlgFile.FileName))
               {
                   if (hashEntries.Contains(lvSums.SelectedItems[0].SubItems[1].Text) == true)
                   {
                       lvSums.SelectedItems[0].SubItems.Add("true");
                       break;
                   }


               }
           }
           catch (Exception ex) { }
       }





但是当我添加else if时,出现false子项目,那么所有子项目但第一个看起来是假的。



有缺陷





But when I add the else if, so that the "false" subitem appears, then all subitems but the first one appear false.

Flawed

private void compareToolStripMenuItem_Click(object sender, EventArgs e)
       {
           try
           {
               foreach (string hashEntries in File.ReadAllLines(dlgFile.FileName))
               {
                   if (hashEntries.Contains(lvSums.SelectedItems[0].SubItems[1].Text) == true)
                   {
                       lvSums.SelectedItems[0].SubItems.Add("true");
                       break;
                   }
                   else if (hashEntries.Contains(lvSums.SelectedItems[0].SubItems[1].Text) != true)
                   {
                       lvSums.SelectedItems[0].SubItems.Add("false");
                       break;
                   }


               }
           }
           catch (Exception ex) { }
       }

推荐答案

不仅有缺陷,而且无瑕疵片段是如此错误,以至于它真的很尴尬解释一下。首先,当包含返回布尔值(而不是可以为空的布尔值?)时,将它与true进行比较是没有意义的或者:
Not only "flawed", but also "flawless" fragment is so wrong that it's really awkward to explain it. First of all, as Contains returns Boolean (and not nullable Boolean?), the is no point comparing it with true or false:
bool something = //... no matter what

//...

// silliest thing:
if (something == true) { /* ... */ }

// should be:
if (something) { /* ... */ }



第二个想法揭示了编程是如何完成的否则如果。该代码相当于


The second think revealing having no clue on how programming is done is "else if". The code is equivalent to

if (hashEntries.Contains(someText))
   // one thing
else if (!hashEntries.Contains(someText))
   // another thing



这里的问题是第二次检查:else if应该替换为if。顺便说一句,如果某些函数或属性计算两次可能会产生一些副作用,这可能会使第二次调用的结果与第一次调用不同,但当然这不是case。



现在,最糟糕的事情是:代码的两个片段都不正确,因为它可能会抛出不需要的异常。比如说, lvSums.SelectedItems [0] 不一定成功,因为所选项目的数量可能为空。如果成功, lvSums.SelectedItems [0] .SubItems [1] 不一定成功,因为子项的数量可以小于2.



通过使用设计师可以找到更多细节。这些问题通常很难理解,但我希望我的答案可以帮助理解什么是真正的缺陷以及你需要学习什么。有缺陷的是你最基本的编码技巧,你需要学习的是:编程需要使用一些大脑并像你所写的一样理解每一个。



-SA


The problem here is the second check: "else if" should be replaced with "if". By the way, it could be not the same if some function or property calculated twice could have some side effect which could make the result of the second call different from first one, but of course this is not the case.

Now, worst thing: both fragments of "code" are incorrect just because it can throw unwanted exception. Say, lvSums.SelectedItems[0] is not necessarily successful, because the number of selected items could be null. If it is successful, lvSums.SelectedItems[0].SubItems[1] is not necessarily successful, because number of sub-items can be less than 2.

Further detail could be found through the use of the designer. Such questions generally make little to know sense, but I hope my answer can help to understand what is really flawed and what you need to learn. The flawed is your very basic coding skills, and what you have to learn is: programming requires using some brain and understanding each like you write.

—SA


同时接受SA和Gustafson的建议。谢谢大家,下面的代码似乎工作

Took both SA and Gustafson advice. Thank you guys, The code below appears to work
private void compareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] hashEntries = File.ReadAllLines(dlgFile.FileName);

            try
            {
                if (lvSums.SelectedItems.Count > 0)
                {// Filename not null
                    if (lvSums.SelectedItems[0].Text != null &&    
                       // Generated Hash not null         
                        lvSums.SelectedItems[0].SubItems[1].Text != null && 
                        // File Size not null    
                        lvSums.SelectedItems[0].SubItems[2].Text != null)   
                    { // Column four for expected boolean                                                      
                            if                          (hashEntries.Contains(lvSums.SelectedItems[0].SubItems[1].Text))
                                lvSums.SelectedItems[0].SubItems.Add("true");
                            if (!hashEntries.Contains(lvSums.SelectedItems[0].SubItems[1].Text))
                                lvSums.SelectedItems[0].SubItems.Add("false");
                    }
                }           
            }
            catch (Exception ex) 
            { 
                MessageBox.Show("Exception: " + ex.ToString(), "Exception Thrown", MessageBoxButtons.OK,MessageBoxIcon.Error);   
            }
        }


这篇关于如果功能在一种情况下完美无缺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆