0

I have a problem very much like the one mentioned here:

ArgumentOutOfRangeException Was Unhandled

I believe that contiguousLines[columnNum].Add(...) is what is causing the error because I am indexing with columnNum

List<line> freeLines = new List<line>();
List<List<line>> contiguousLines = new List<List<line>>();

while(freeLines.Count > 0)
{
    int columnNum = contiguousLines.Count;
    contiguousLines[columnNum].Add(freeLines[0]);
    freeLines.RemoveAt(0);

    for(int i = 0; i < freeLines.Count; i++)
    {
        int last = contiguousLines[columnNum].Count;
        if(contiguousLines[columnNum][last].upDown(freeLines[i]))
        {
            contiguousLines[columnNum].Add(freeLines[i]);
            freeLines.RemoveAt(i);
            i = -1;
        }
    }
    // Further code that pulls individual elements from freeLines and
    // is intended to place them into contiguousLines.

}

The function upDown just compares Start and End points of the lines to see if one (freeLines[i]) is the downstream of the other (contiguousLines[columnNum]).

System.ArgumentOutOfRangeException was unhandled Message=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

What is the proper syntax when dealing with a List of Lists?

(Note: I don't often program in C# and this project is something I wrote and have working in C++ only to be later informed C# would play better with the rest of the utilities for my job. In C++ I used vectors for my containers, but apparently copy/pasting the logic won't work as there is some nuance of Lists that I am unaware of.) I suppose it is also possible to just make a ContiguousLine class that holds a list of Lines, then add to a List<ContiguousLine> from freeLines. Even if that were to be a better solution, I am still curious why I can not address a List of Lists of Lines in this way.

Community
  • 1
  • 1
AChrapko
  • 166
  • 2
  • 2
  • 13
  • Understand the things ... dont just copy pasre. – Guanxi Apr 17 '15 at 03:53
  • I was copy and pasting my own C++ code- I was clearly posting this question so as to understand the C# syntax of Lists. You also posted that comment after I accepted the answer for some reason... – AChrapko Apr 17 '15 at 03:54
  • I am also surprised to see people down voting the question, it's more like a hoax – uowzd01 Apr 17 '15 at 03:55

2 Answers2

3
int last = contiguousLines[columnNum].Count;

As lists are 0-indexed, you're 1 over.

McAden
  • 13,714
  • 5
  • 37
  • 63
  • Err yes, I suppose I should have copied and pasted my code rather than retyping an example- I did miss the -1 there: however the ArgumentOutOfRangeException happens 5 lines before that- I just added that code to introduce the fact that I am removing things from freeList at some point – AChrapko Apr 17 '15 at 03:34
1

you need to add a List first before access the column

contiguousLines.Add(new List<line>());
contiguousLines[columnNum].Add(freeLines[0]);
uowzd01
  • 1,015
  • 10
  • 19
  • When I run it in a debugger I can see that `columnNum` = 0 because contiguousLines.Count has nothing in it in the first pass. – AChrapko Apr 17 '15 at 03:31