I’m iterating through a string list (storedProcedures) and splitting each list item into an array. I would like to then access the array elements using specific indices. I am getting an out of range exception as soon as I try to access the second element of the array.
string[] myArray = new string[4];
foreach (string procedure in storedProcedures)
{
myArray = procedure.Split(',');
foreach (string index in myArray)
{
Console.WriteLine(index);
}
for (int i = 0; i < myArray.Length; i++)
{
Console.WriteLine(myArray[i]);
}
Console.WriteLine(myArray[0]);
Console.WriteLine(myArray[1]); <---- out of range exception here
Console.WriteLine(myArray[2]);
Console.WriteLine(myArray[3]);
}
Accessing the array and printing its contents using the ‘foreach’ and ‘for’ loops work fine, so I’m not sure why accessing the array by specifying the indices directly doesn’t work?
This is useless:
because this throws your array away:
Your problem is that one of
storedProceduresdoes not have a comma in it.