-4

I am trying to load in data field names into my program from a text file, and store them into a variable, an array.

The field names are to be used to sort and print data from other files into an organized new document.

I'm having a lot of trouble specifically with storing the fields from the file into an array.

The data fields text file is organized in a list manner, with each field being on its own row.

I went with creating a while loop that runs until the Peek() of the StreamReader is equal to -1.

I nested a for loop within the while loop that increments the index integer variable by 1 until it's less than or equal to the total amount of rows in the text file.

It also uses ReadLine() to store a row of the text document into the array at the specific index. I thought using that in the for loop would cycle through each row and store what it needs to store.

I will need to use the fields within a dictionary so that I can use the fields as keys and the data from other documents as the values for the dictionary when I get around to displaying that information.

I thought the way I had done it would have the necessary measures to avoid the IndexOutOfRangeException, but I guess that is not the case.

I would appreciate anyone willing to help me with this. I apologize if anything is unclear and will try to clarify things if need be.

If my attempted logic explanation was terrible, here's the code:

    class Program
{
    protected static string[] dataFields = new string[] { };

    static void Main(string[] args)
    {
        int iIndex;

        using (StreamReader titleStream = new StreamReader(@"..\..\DataFieldsLayout.txt"))
        {
            while (titleStream.Peek() > -1)
            {
                for (iIndex = 0; iIndex <= 150; iIndex++)
                {
                    // where the exception occurs
                    dataFields[iIndex] = titleStream.ReadLine();
                }
            }
        }

        // test
        Console.WriteLine(dataFields[0]);
        Console.WriteLine(dataFields[1]);
        Console.WriteLine(dataFields[2]);
        Console.WriteLine(dataFields[3]);
        Console.WriteLine(dataFields[4]);
    }
}
Snoop
  • 1,046
  • 1
  • 13
  • 33
NerveSinge
  • 39
  • 4
  • Possible duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – Matt Rowland Jun 18 '16 at 22:10
  • You have declared an array of strings with zero elements, It is impossible to use any kind of index on this array. – Steve Jun 18 '16 at 22:10
  • On another point. Are you sure that your file always contains exactly 151 rows or multiple of 151? – Steve Jun 18 '16 at 22:13
  • Looking at it in Notepad++ the first line is at 1 and the last line is at 150; they are exact rows. – NerveSinge Jun 18 '16 at 22:29

1 Answers1

0

This is because when you define an array you have to make room for each element. So, when you declare the array as you have done you are not giving it any space to accept new data. You might want to declare your array in the following way (so that it has enough space for all of the data):

protected static string[] dataFields = new string[151];

You are reading up to 151 elements (because your <= 150 condition), so you want to give the appropriate amount of space.

Snoop
  • 1,046
  • 1
  • 13
  • 33
  • Also consider using a List instead of a string[]. The number of times you will know with absolute confidence how many items your collection will contain is very, very small, and if you actually care about the performance overhead of a List, then you should probably be using C/C++/Rust/Something not garbage collected anyway. – Nick Bailey Jun 18 '16 at 22:13
  • @NickBailey Yeah you are right, especially if you don't know how many items you want. Then you can just `.ToArray()` the list later on. – Snoop Jun 18 '16 at 22:14
  • Yeah, I'm not sure I've ever used an array that wasn't readonly. – Nick Bailey Jun 18 '16 at 22:15
  • @NickBailey You know what though, whenever I have to return more than one item I always return an array, never a list. I think It's me trying to be a language purist though. – Snoop Jun 18 '16 at 22:16
  • @NickBailey I don't see how, but apparently it's not a good thing to return arrays? http://stackoverflow.com/questions/1274914/is-it-bad-form-to-return-arrays-in-c-should-i-return-listt – Snoop Jun 18 '16 at 22:18
  • I had originally avoided doing a list because I thought I was specifically only instructed to use arrays, but I had misread that part of the instructions for this assignment. Adding 151 made the code run successfully, so I consider this answer to solving my question. I will definitely try the list way of doing it, however. – NerveSinge Jun 18 '16 at 22:40
  • @NerveSinge Nick's comments about lists are correct. You may even want to do something like `while (list.Count < 151) { list.Add(titleStream.ReadLine(); }`. – Snoop Jun 18 '16 at 22:42