Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multidimensional Arrays

Posted on 2008-10-08
10
Medium Priority
?
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Stressed Out?

Watch some penguins on the livecam!

 
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 1000 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 1000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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