Reputation: 83
I don't understand why I am getting this error because my multi-dimensional array should function fine but it isn't working in this case due to the listed error...I am very frustrated.
error is: Wrong number of indices inside []; expected 2
This is what I have:
public static void DisplayTopScore(string username, double score)
{
string[] highscores = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
string[,] Temphighscores = new string[10, 2];
string[] TempScoresToSplit;
int counter=0;
foreach (string highScore in highscores)
{
TempScoresToSplit = highScore.Split(' ');
Temphighscores[counter][0]= TempScoresToSplit[0];
Temphighscores[counter][1]= TempScoresToSplit[1];
counter++;
}
}
}
The place where it says wrong number of indices are at these 2 lines:
Temphighscores[counter][0]= TempScoresToSplit[0];
Temphighscores[counter][1]= TempScoresToSplit[1];
Upvotes: 4
Views: 24479
Reputation: 1
Initialize array in this way:
string[][] Temphighscores = new string[10][];
Upvotes: 0
Reputation: 126042
Try:
Temphighscores[counter, 0] = TempScoresToSplit[1];
Temphighscores[counter, 1] = TempScoresToSplit[1];
instead.
The MSDN article on multidimensional arrays is probably worth a read.
Upvotes: 10