Multidimensional Arrays

I have created a multidimensional array:

string[,] strArray = new string[0,0];

I am tring to assign values to the array:

strArray [0, 0] = "Value1";

I am receiving the following error when assigning the values - "Index was outside the bounds of the array."

How do I resolve?
LVL 2
Who is Participating?

Commented:
Do you know how many rows/columns you're going to need?

If so, something like

string[][] strArr = new string[MAX_ROWS][MAX_COLUMNS];

is what you need, where MAX_ROWS and MAX_COLUMNS are > 0.

Then you can set with

strArr[x][y] = "valxy"  where MAX_ROWS > x >= 0 and MAX_COLUMNS > y >= 0
0

Commented:
Hello CipherIS,

the problem is that your are has size [0, 0]. It is defined to contain 0 elements in both dimensions

Regards,

xRalf
0

Commented:
Hello CipherIS,

>string[,] strArray = new string[0,0];
Your array has 0 rows and 0 cols

change it to
string[,] strArray = new string[1,1];

Regards,

sunnycoder
0

Software ArchitectCommented:
You are declaring a jagged array, would be better a traditional arrray as:

string[][] strArray = new string[1][1];
0

Author Commented:
Ok this works with one value:
>string[,] strArray = new string[0,0];
Your array has 0 rows and 0 cols

change it to
string[,] strArray = new string[1,1];

strArray [0, 0] = "Value1";
How do I add a second value:
strArray[0,1] = "Value2";    ?????
0

Commented:
If you declare an with dimensions [x][y] then valid indices you can access are from [0][0] to [x-1][y-1]

>strArray[0,1] = "Value2";    ?????
So if you need to access [0,1] then you need to declare your array with minimum dimensions [1][2].

as Atbenning pointed out - new string[MAX_ROWS][MAX_COLUMNS]; is what you need.
0

Commented:
I am sorry for not having dropped by earlier ... a split would be fair.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.