-3

I'm getting System.ArgumentOutOfRangeException error message when I'm trying to substring a year off of a string which is in DateTime format ("mm/dd/yyyy hh:mm:ss xx") and assign it to a List / ArrayList.

        for(int i = 0; i < elementCounter; i++)
        {
            String stringDate = Convert.ToString(DatumPocetka[i]);
            DPGodina[i] = stringDate.Substring(stringDate.Length - 16, 4);
        }

DatumPocetka is an ArrayList with string type values which are in DateTime format I mentioned earlier.

I have to calculate the starting character from the back of the string though. I don't know the index where the year starts when I'm calculating it from the beginning because days and months can be single and double digits. It's easier to calculate it from the back because the time part of the string stays static. ("mm/dd/yyyy hh:mm:ss xx") this part of the string always has 16 characters, so that's why I'm subtracting the length by 16.

Even Visual Studio says that stringDate.Length is equal to 21, but I'm still getting the ArgumentOutOfRangeException on the DPGodina[i] = stringDate.Substring(stringDate.Length - 16, 4); line .

I have tried doing this with List<DateTime> using name.Year.ToString(); but that did not work either.

mjwills
  • 23,389
  • 6
  • 40
  • 63
Marko
  • 5
  • 2
  • 3
    They aren't in range. Simple. What is the value of `stringDate` when you get this exception? – ProgrammingLlama Jan 10 '19 at 00:55
  • 3
    Use `DatumPocetka[i].Year` – Backs Jan 10 '19 at 00:58
  • 3
    `...I'm pretty sure the arguments are in range` The illusion of knowledge is a dangerous thing. Debug your code to find out for sure what and where things went off the rails: **[Navigating through Code using the Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Jan 10 '19 at 01:00
  • 1
    Question: Do you pre-fill your ArrayList or List? I suspect there are 0 elements,s o you're trying to use an index that's out of range. The code you have demonstrated works fine for me when used with an array. – ProgrammingLlama Jan 10 '19 at 01:30
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – ProgrammingLlama Jan 10 '19 at 01:37
  • 1
    See [this answer](https://stackoverflow.com/a/20940980/3181933) under "How it applies to List?" – ProgrammingLlama Jan 10 '19 at 01:37

1 Answers1

2

This code:

for(int i = 0; i < elementCounter; i++)
{
    String stringDate = Convert.ToString(DatumPocetka[i]);
    DPGodina[i] = stringDate.Substring(stringDate.Length - 16, 4);
}

could be replaced with:

var DPGodina = DatumPocetka.Select(z => z.Year.ToString()).ToList();

if you'd like to remove the loop altogether. This will also avoid the need to worry about string parsing, and loop / list index issues.

You will also need to remove your existing DPGodina variable declaration.

mjwills
  • 23,389
  • 6
  • 40
  • 63