0

I'm working in this code for setting values in a specific part of a [5,5] 2D array from keyboard, for example ad an "x" in column 2 row 3, and it's almost done but I keep getting an error. Let´s begin with the part of the code I have problems with:

int[,] data = new int[5, 5]; 

public void cargar()
{
   string[] input = Console.ReadLine().Split('=');
   string[] coordinates = input[0].Split(',');
   int[] intCoordinates = coordinates.Select(s => int.Parse(s)).ToArray();
   data[intCoordinates[0]][intCoordinates[1]] = int.Parse(input[1]);  
}

The message refers to the 10th line, it´s about "data" (it´s the name of the array):

"Incorrect index number inside []. 2 was expected".

It´s translated from Spanish, so I think that's what is says. How could I fix this?

rene
  • 41,474
  • 78
  • 114
  • 152
Gabbo2483
  • 47
  • 7

1 Answers1

4

You're using a multidimensional array ([,]) but then trying to access it like its an array of arrays ([][]) - also called jagged arrays.

You need to access it like data[intCoordinates[0],intCoordinates[1]] instead.

The error message is telling you that you're only using one indexer when it is expecting two of them.

See this MSDN article on multidimensional arrays, this one on jagged arrays and this StackOverflow question/answer.

Community
  • 1
  • 1
Tim
  • 14,999
  • 1
  • 45
  • 68