2

I am trying to make a C# for loop that will loop through an array of items and print the first date of the user. Below is the code for the array (which i think is correct).

string[,] arrHolidays = new string[2, 3];
arrHolidays[0, 0] = "10/05/2015";
arrHolidays[0, 1] = "15/05/2015";
arrHolidays[0, 2] = "Danny";
arrHolidays[1, 0] = "20/05/2015";
arrHolidays[1, 1] = "22/05/2015";
arrHolidays[1, 2] = "Kieran";

Below is the code for the For loop (which is throwing the error)

for(int i = 0; i < arrHolidays.Length; i++)
{
    Console.WriteLine(arrHolidays[i, 0]);            
}

Index out of range exception was unhandled. this is the error I am recieving. When it throws the error and I check the value of 1 it is 2 if this is any help in resolving it. What I am expecting to see is this in the console app: 10/05/2015 20/05/2015

INB4 I have hardly any c# experience this is my first project, so please explain any help :)

Kieranmv95
  • 828
  • 4
  • 14
  • 31
  • 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) – Adriano Repetti Mar 20 '14 at 11:30
  • Maybe the three down-voters on this question could explain what they think is wrong with it? – Surfbutler Mar 20 '14 at 11:43

2 Answers2

14

You must use GetLength method, not the Length property.

The Length property for a multi-dimensional array returns the total count of all the items in the array. The GetLength method returns the number of size of a given dimension (where dimension is specified as a zero-based index).

So, arrHolidays.Length will return 6.

Change for loop to:

  for (int i = 0; i < arrHolidays.GetLength(0); i++)
  {
        Console.WriteLine(arrHolidays[i, 0]);
  }
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • +1, but still a small remark : things would be easier if the OP is using objects to save up his data. – KarelG Mar 20 '14 at 11:33
  • yes debugging it does return six, which is every item. how would i make it return only the section i want the `[i,0]` – Kieranmv95 Mar 20 '14 at 11:36
  • @FarhadJabiyev it means before anyone says anything i know its an easy problem i'm just new to C# so please explain responses – Kieranmv95 Mar 20 '14 at 11:45
  • @Kieranmv95 Not any problem. I will be glad to help you. Just comment here exactly what you want from me to explain. – Farhad Jabiyev Mar 20 '14 at 11:47
  • @FarhadJabiyev your method works perfect, but when i try to implement the same thing for the other set of dates n a seperate for i get the error again :(. `for (int i = 0; i < arrHolidays.GetLength(1); i++) { Console.WriteLine("EndDate" + arrHolidays[i, 1]); }` This **should** return the second set of dates i thought – Kieranmv95 Mar 20 '14 at 11:50
  • @Kieranmv95 Because `arrHolidays.GetLength(1)` returns 3. Your array's is 2x3. And there is not exist arrHolidays[2,1]; You must write: `for (int i = 0; i < arrHolidays.GetLength(0); i++){Console.WriteLine(arrHolidays[i, 1]);}` – Farhad Jabiyev Mar 20 '14 at 11:54
  • @Kieranmv95 )) It can happen to anyone. I am glad to help. – Farhad Jabiyev Mar 20 '14 at 11:56
2

Array.GetLength(int dimension)

In your case, you want to get the length of the first dimension, so arrHolidays.GetLength(0).

myermian
  • 31,823
  • 24
  • 123
  • 215