Skip to content

Need help on index was outside..

Prince

Asked Feb 3, 2010, 4:48 AM EST2 answers
Share
LinkedInX
this is part of my code ,though the program  run(not really correct
)also have this message "Index was outside the bounds of the array"
using (StreamReader sr = new StreamReader("Date.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Date mydate = new Date();
int monthTemp;
int dayTemp;
int yearTemp;
String[] str = line.Split(' ');
Int32.TryParse(str[0], out monthTemp);
mydate.Month = monthTemp;
Int32.TryParse(str[1], out dayTemp);
mydate.Day = dayTemp;
Int32.TryParse(str[2], out yearTemp);
mydate.Year = yearTemp;

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mahesh Chand
    0
    How many items do you have in your array?
    String[] str = line.Split(' ');

    This error occurs when you do not enough items in an array. For example, if you have only two items in the array at 0th and 1st position and try to do this:

    Int32.TryParse(str[2], out yearTemp);

    In the above code you are actually referencing 3rd item of array.

    Debug your code and see how many items you have in str.
    0
  • 0
    thanx. I have 3 items in my list: day,month and year in this format e.g  4 15 2007
    0