none
Problem nesting IF statements

    Question

  • Hi

    I have written this code:

        private void btnSearchFile_Click(object sender, EventArgs e)
            {
                string file = openFileDialog1.FileName;
                List<string> theWholeFile = File.ReadLines(file).ToList();
                foreach (string line in theWholeFile)
                {
                 
                    if (line.Contains(txtInputSearch.Text))
                    {

                        listBox1.Items.Add(line);
                    }

    the problem is if I wish to nest an if statement after if(line.Contains(txtInputSearch.Text)) nothing happens. For example, in the search box I am only searching 3 letter codes like ABC or FDD then all lines that contain FDD for example are returned. Now if I add the following:

    if (line.Contains("Final"))
                    {
                        listBox1.Items.Add(line);
                    }

    It only returns the same data as the first time it ran. Is there a problem with nesting if statements when taking input from a text box? 



    Thursday, March 30, 2017 4:01 PM

Answers

  • Hi

    Thanks for your reply, your answer is the same as the way I wrote it in my question and although its not the answer I'm looking for it has made me look at my logic and realise the fault is there. I was reading lines that contained the letters in my textbox then trying to search for other keywords on other lines, it's not showing because the keyword lines do not contain the initial 3 letter word search, they are on the next line.

    Thank you all for your time.



    Friday, March 31, 2017 8:45 AM

All replies

  • There is certainly nothing wrong with nesting 'if' statements but may need more of your actual final code to see what problem you are actually getting.

    For example:

    if (line.Contains(txtInputSearch.Text))
    {
       if (line.Contains("Final"))
       {
            listBox1.Items.Add(line);
       }
    }

    This will have the effect of only adding line to the listbox if the line contains both the textbox text and the value "Final".

    When pasting your code, use the "insert code block" button in the editor (2nd button from the right). Makes it much easier to read!

    Thursday, March 30, 2017 4:08 PM
  • Just a further clarification to RJP1973's post.

    Under that scenario, if all the lines that contain "FDD" (or whatever you are searching for) also contain the word "Final", the extra "if" will make no difference. For example, suppose the file looks like this...

    A ABC Start
    B FDD Final
    C FDD Final
    D FDD Final
    E XYZ Final

    ...then with or without the extra "if" you will get lines B, C, and D.

    If, however, your intention is to get lines B, C, D, and E (the ones that contain "FDD" or "Final"), then you need a single "if" with both conditions.

    if (line.Contains(txtInputSearch.Text) || line.Contains("Final"))
    {
    
         listBox1.Items.Add(line);
    
    }

    Thursday, March 30, 2017 10:40 PM
  • Hi

    Thanks for your reply, your answer is the same as the way I wrote it in my question and although its not the answer I'm looking for it has made me look at my logic and realise the fault is there. I was reading lines that contained the letters in my textbox then trying to search for other keywords on other lines, it's not showing because the keyword lines do not contain the initial 3 letter word search, they are on the next line.

    Thank you all for your time.



    Friday, March 31, 2017 8:45 AM