I'm trying to load a 2 dimensional array with the first dimension containing the characters 1-12 and the second the characters 1-31.
string[,] holiDays = new string[,] {{ "1","2","3","4","5","6","7","8",
"9","10","11","12" },{ "1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20","21","22",
"23","24","25","26","27","28","29","30","31" }};
I get the folowing error:
Invalid rank specifier: expected ',' or ']' (CS0178)
If I trim each of the array dimension values to seven, the error goes away. As soon as I add the eighth value in either dimension, the error returns.
For example, this gets no error:
string[,] holiDay2 = new string[,] {{ "1","2","3","4","5","6","7"},
{ "1","2","3","4","5","6","7"}};
These get the error:
string[,] holiDay2 = new string[,] {{ "1","2","3","4","5","6","7","8"},
{ "1","2","3","4","5","6","7"}};
string[,] holiDay2 = new string[,] {{ "1","2","3","4","5","6","7"},
{ "1","2","3","4","5","6","7","8"}};
Any ideas on this?
Loading
Stephen GoldenPosted Dec 19, 2007, 6:13 PM
Thanks again!
AlanPosted Dec 19, 2007, 5:34 PM
If the dimensions are of unequal length then you need to create a 'jagged' array rather than a rectangular array.
For example:
string[][] holidays = new string[2][];
holidays[0] = new string[12]{"1","2","3","4","5","6","7","8","9","10","11","12"};
holidays[1] = new string[31]{ "1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20","21","22",
"23","24","25","26","27","28","29","30","31" };
Console.WriteLine(holidays[1][11]); // 12
A neater way of doing the initialization would be like this:
string[][] holidays = new string[2][];
holidays[0] = new string[12];
holidays[1] = new string[31];
for (int i = 1; i < 13 ; i++)
{
holidays[0][i-1] = i.ToString();
}
for (int i = 1; i < 32 ; i++)
{
holidays[1][i-1] = i.ToString();
}
Console.WriteLine(holidays[1][11]); // 12