Solved

Multidimensional Arrays

Posted on 2008-10-08
10
616 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:CipherIS
10 Comments
 
LVL 6

Expert Comment

by:xRalf
ID: 22669150
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22669154
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22669814
You are declaring a jagged array, would be better a traditional arrray as:

string[][] strArray = new string[1][1];
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:CipherIS
ID: 22671414
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
 
LVL 2

Accepted Solution

by:
Atbenning earned 250 total points
ID: 22672392
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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 250 total points
ID: 22674295
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22876276
I am sorry for not having dropped by earlier ... a split would be fair.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Achieve json result 2 65
asp.net mvc input file 2 32
Need to pass a connection through class/Method 1 40
Word Directory is not in the drop down list 4 25
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now