Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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) { }
       }


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

Flawed

C#
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) { }
       }
Posted
Comments
Herboren 31-May-15 19:35pm    
It comparing other entries in the file to the selected text which is why it is coming back false, even though it exists.
gggustafson 31-May-15 22:35pm    
Let me get this right. First you are asking if hashEntries contains the text lvSums.SelectedItems[0].SubItems[1].Text. Contains returns a bool so you do not need (in C#) the == true and != true. The else if should just be else. What's the foreach for? You only execute it once because you break on each side of the if/else.

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:
C#
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
C#
if (hashEntries.Contains(someText))
   // one thing
else if (!hashEntries.Contains(someText))
   // another thing

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
 
Share this answer
 
Took both SA and Gustafson advice. Thank you guys, The code below appears to work
C#
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);   
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900