0
string path = "abc\\ab.txt";
string[] memo = System.IO.File.ReadAllLines(path);
int totalLines = textBox2.Lines.Length;
string lastLine = textBox2.Lines[totalLines - 1];
for (int i = 0; i < 6; i++)
{
    string a = memo[i];
    if (a.Contains(lastLine))
    {
        textBox2.Text = (textBox2.Text + Environment.NewLine + memo[i + 1]);
        break;
    }
    else
    {
        MessageBox.Show("not found");
    }
}

if else doesn't work.

If I write the code like under:

else
{
    MessageBox.Show("cs0162 c# unreachable code detected");
    break;
}

The warning message is:

cs0162 c# unreachable code detected"come out.

mr.G
  • 109
  • 1
  • 7
  • Does this answer your question? [Break loop once expected line is found](https://stackoverflow.com/questions/65267964/break-loop-once-expected-line-is-found) –  Dec 13 '20 at 05:43
  • Thank you for taking the time to share your question. What you asking for is unclear. What is your goal & difficulty? What have you done so far? Please try to better explain your issue, dev env, data types & expected result, as well as to share more or less code (no screenshot), images or sketches of screens, user stories or scenario diagrams. To help you improve your requests please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) & [Writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question). –  Dec 13 '20 at 05:44
  • 1
    @OlivierRogier So sorry. My goal is When the button is clicked, the entire notepad file is scanned. If there is a line containing the text in Text Box 1, the next line of the line is displayed in Text Box 2, and if there is no line included, a message "not found" displayed Since I am not sure how to read the entire line of notepad and find out which line is the correct line, I saved 5 lines in the notepad and checked only 5 lines. However, when writing the code above, it works normally only when the first line of Notepad and is entered. – mr.G Dec 13 '20 at 06:49

2 Answers2

1

As I understood your goal, you can try that using Linq:

string filePath = "d:\\test.txt";
var fileLines = File.ReadAllLines(filePath).ToList();

var foundLine = fileLines.Select((line, index) => new { line, index })
                         .Where(pair => pair.line.Contains(textBox1.Text))
                         .FirstOrDefault();

if ( foundLine != null && foundLine.index < fileLines.Count - 1 )
  textBox2.Text = fileLines[foundLine.index + 1];
else
  textBox2.Text = "Not found";

We select a pair of { string line, int index } for each line of the contents of the file. That we filter to match the contents of the text box. And we take the first found or null if it is not found. Then we check the result as well as the number of lines in the file and we display the expected output. And if we found the last line, we display not found.

At this point, the string comparison is also case sensitive and requires full equality.

If you need to find by contains you can write:

Where(pair => pair.line.Contains(textBox1.Text))

And to test case insensitive you can for example write:

Where(pair => pair.line.IndexOf(textBox1.Text, StringComparison.OrdinalIgnoreCase) >= 0 )

Also for start with:

Where(pair => pair.line.IndexOf(textBox1.Text, StringComparison.OrdinalIgnoreCase) == 0)
-1

I understand you get this warning with this code:

{
string a = memo[i];
if (a.Contains(lastLine))
{
    textBox2.Text = (textBox2.Text + Environment.NewLine + memo[i + 1]);
    break;
}
else
{
    MessageBox.Show("not found");
    break;
}

}

If so, you get the warning because there are two options on if, the condition is true or the condition is false. You are breaking out of the loop in both cases (if for condition true and else for condition false, therefore, the loop always runs only one single iteration. The other iterations are unreachable.

Hope I understood your question correctly.

Julio Cachay
  • 780
  • 5
  • 10
  • Adding a break causes the loop to only iterate once, so it's unnecessary. I think the problem is that the OP is trying to do something incorrectly and the code is weird, so you need to tell us the goal before you can help. –  Dec 13 '20 at 05:50
  • I didn’t suggest to add a break on the else block. I used that code snipped to show what I think he is doing and explain why that was problematic. – Julio Cachay Dec 13 '20 at 05:52
  • You just need to remove the break from the loop, because right now, if it reads a line, it breaks out of the loop and never gets to read the rest of the lines. So, just try removing the break. – Julio Cachay Dec 13 '20 at 06:27
  • That's not work dude... – mr.G Dec 13 '20 at 06:35