Solved

Multidimensional Arrays

Posted on 2008-10-08
10
617 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

9 Experts available now in Live!

Get 1:1 Help Now