2 Replies - 1495 Views - Last Post: 30 October 2011 - 06:47 AM Rate Topic: -----

#1 Igit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 11

RE:index out of range exception was unhandled

Posted 30 October 2011 - 06:18 AM

I get an "index out of range exception was unhandled-Index was outside the bounds of the array" while debbuging.
I have tried changing the separation symbols between the arrays in the file but did not work

 StreamReader infile = new StreamReader("G:\\Countries.txt");
            while (infile.Peek() != -1)
            {
                string currentline = infile.ReadLine();
                string[] splitstring = currentline.Split(',');

                ModCountry newCountry = new ModCountry();
                //add object to list
                newCountry.CountryName = splitstring[0];
                newCountry.CountryArea = Convert.ToString(splitstring[1]);//I get an error on this line
                newCountry.CountryPopulation = Convert.ToString(splitstring[2]);
                newCountry.CountryGdp = Convert.ToString(splitstring[3]);
                newCountry.CountryDateIn = Convert.ToDateTime(splitstring[4]);

                countrylist.Add(newCountry); count++;
                CountryListBox.Items.Add(splitstring[0]);


            }
            infile.Close();



Is This A Good Question/Topic? 0
  • +

Replies To: RE:index out of range exception was unhandled

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5583
  • View blog
  • Posts: 11,943
  • Joined: 02-June 10

Re: RE:index out of range exception was unhandled

Posted 30 October 2011 - 06:39 AM

This is VERY basic debugging. Basically you need to put a breakpoint in the routine and check the values of your variables with each iteration of the loop.

Attached Image

Keep in mind that collections are all zero-indexed. So if you have 10 items they are 0-9. If you try to access item 10 it is beyond the range of the collection.

You aren't doing any range checking. Your line 13 for example assumes the split in line 5 was successful. What if it wasn't? What if there is no element 4 in the returned collection because there weren't enough commas in the source text?

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


See FAQ #5 for debugging tutorials.


FAQ (Frequently Asked Questions - Updated Oct 2011

Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5583
  • View blog
  • Posts: 11,943
  • Joined: 02-June 10

Re: RE:index out of range exception was unhandled

Posted 30 October 2011 - 06:47 AM

Also, String.split returns a string array. There is no need to convert.ToString the results as you have done.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1