0

I have been working on a windows from application abd I seem to have run into a problem where I am getting Index as out of rang of array when trying to add a text box line to a list:

My current code is as follows:

 if (NameTB.Lines.Count() > 0)
                for (int i = 1; 1 <= NameTB.Lines.Count(); i++)
                    nameList.Add(NameTB.Lines[i].Replace(Environment.NewLine, "").Trim());

I have also tried this:

    if (NameTB.Lines.Length > 0)
        for (int i = 1; 1 <= NameTB.Lines.Length; i++)
            nameList.Add(NameTB.Text[i - 1].ToString().Replace(Environment.NewLine, "").Trim());

and

        if (NameTB.Lines.Count() > 0)
            for (int i = 1; 1 <= NameTB.Lines.Count(); i++)
                nameList.Add(Convert.ToString(i).Replace(Environment.NewLine, "").Trim());

Also though the last example does not give the index error but gets stuck in a loop. I have done something like this before but using WPF and TextBox.GetLineText but to sure how to achive the same thing in Winforms

  • In `for` loop condition `1 <= NameTB.Lines.Count` will always return `true` - so you end up with infinite loop – Fabio Oct 06 '17 at 07:22
  • You can get all lines by `var lines = NameTB.Text.Split(Environment.NewLine).Select(line => line.Trim()).ToArray();` – Fabio Oct 06 '17 at 07:27

4 Answers4

2

Use this:

if (NameTB.Lines.Count() > 0)
    for (int i = 0; i < NameTB.Lines.Count(); i++) //or i <= NameTB.Lines.Count() - 1
        nameList.Add(NameTB.Lines[i].Replace(Environment.NewLine, "").Trim());

Indexing in c# are 0-based, so you need to start from 0 (of course if you don't want to start from second element).

Of course your infinite loop and IndexOutOfRangeException problems caused by 1 <= NameTB.Lines.Count(); condition, your i increasing endlessly, but NameTB.Lines has limited amount of elements, so you should use i instead of 1.

But only changing 1 to i will not solve your problem, because elements count in any array/collection is always bigger than the last element's index, so you need to change

i <= NameTB.Lines.Count();

to

i < NameTB.Lines.Count(); //or i <= NameTB.Lines.Count() - 1;
SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • you really should mention that the error comes about, because the cacel condition in OP's for loop is a comparison of `Count` with the number `1` and not the indexing variable. – Mong Zhu Oct 06 '17 at 07:36
  • " it can cause an infinite loop, but it will never throw IndexOutOfRangeException" Yes, this is exactly what the result of an endless incrementation of `i` will be. if the loop never stops, `i` will be at some point larger than `Count` – Mong Zhu Oct 06 '17 at 07:41
  • I am downvoting, because your answer does not address the core of the problem. The core is the cancel condition of the loop, not the starting value. Your post has a working code, but you failed to explain why the problem arose in the first place – Mong Zhu Oct 06 '17 at 07:42
  • Might be worth noticing the `if` isn't necessary as it is contained in the *continue condition* of the `for` loop – Rafalon Oct 29 '17 at 12:26
1
1 <= NameTB.Lines.Count();

Isnt that always true, as you dont change the Count of the Lines (and you shouldnt eitherway)?

I guess you want to loop until

i < NameTB.Lines.Count();

And want to start at 0, as the first index in an array is 0

TheSkimek
  • 334
  • 1
  • 7
  • 1
    In this case he will also get `IndexOutOfRangeException`, because of `<=` (Only second version will work) – SᴇM Oct 06 '17 at 07:13
1

Note that arrays are zero-based, and your second argument seems creating an infinite loop which exceeds the maximum array index of Lines. The correct for-loop construction should be like this:

if (NameTB.Lines.Count() > 0) 
{
    for (int i = 0; i < NameTB.Lines.Count(); i++)
    {
        nameList.Add(NameTB.Lines[i].Replace(Environment.NewLine, "").Trim());
    }
}

See also:

What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
-1

Try this:

if (NameTB.Lines.Count() > 0)
                for (int i = 1; i <= NameTB.Lines.Count() - 1; i++)
                    nameList.Add(NameTB.Lines[i-1].Replace(Environment.NewLine, "").Trim());
Bungysheep
  • 40
  • 7
  • 1
    You should explain your answer. Also, I generally use `i < [..].Count()` instead of `i <= [..].Count() - 1` which seems more messy to me. – Rafalon Oct 06 '17 at 07:27
  • Yeah, you can use Count() or Count() - 1. I just want to make it is similar as the original code, so you can see the different. ;) – Bungysheep Oct 29 '17 at 09:00