473,411 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Multidimensional array


Hi,

How can I declare and instantiate a multidimensional array that I need to return after having populated it from a DB. Here is the code:

string[,] g = new string[2,];
int i = 0;
while(rdr.Read())
{
g[0, i] = rdr.GetString(1) + " " + rdr.GetString(2);
g[1, i] = Convert.ToString(rdr.GetInt32(0)) + "~" +
rdr.GetString(3) + "~" +
rdr.GetString(4) + "~" +
rdr.GetString(5) + "~";
i++;
}

What is wrong with the above code? I try everything...

Thanks.
Mike
Nov 16 '05 #1
2 7551
Arrays declared with square brackets are fixed-sized, not dynamic in the way you're trying to treat it. Use an ArrayList to dynamically add the rows to the array and then convert it over to a fixed-size array. For example,

ArrayList ga = new ArrayList();

int i = 0;
while (rdr.Read())
{
string[] gi = new string[2];
gi[0] = rdr.GetString(1) + " " + rdr.GetString(2);
gi[1] = Convert.ToString(rdr.GetInt32(0)) + "~" +
rdr.GetString(3) + "~" +
rdr.GetString(4) + "~" +
rdr.GetString(5) + "~" };
ga.Add(gi);
}

string[][] g = (string[][]) ga.ToArray(typeof(string[]));

Ken
"Mike" <no****@nospam.com> wrote in message news:uQ*************@TK2MSFTNGP12.phx.gbl...

Hi,

How can I declare and instantiate a multidimensional array that I need to return after having populated it from a DB. Here is the code:

string[,] g = new string[2,];
int i = 0;
while(rdr.Read())
{
g[0, i] = rdr.GetString(1) + " " + rdr.GetString(2);
g[1, i] = Convert.ToString(rdr.GetInt32(0)) + "~" +
rdr.GetString(3) + "~" +
rdr.GetString(4) + "~" +
rdr.GetString(5) + "~";
i++;
}

What is wrong with the above code? I try everything...

Thanks.
Mike
Nov 16 '05 #2

Thanks Ken.



"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:#f**************@TK2MSFTNGP10.phx.gbl...
Arrays declared with square brackets are fixed-sized, not dynamic in the way you're trying to treat it. Use an ArrayList to dynamically add the rows to the array and then convert it over to a fixed-size array. For example,

ArrayList ga = new ArrayList();

int i = 0;
while (rdr.Read())
{
string[] gi = new string[2];
gi[0] = rdr.GetString(1) + " " + rdr.GetString(2);
gi[1] = Convert.ToString(rdr.GetInt32(0)) + "~" +
rdr.GetString(3) + "~" +
rdr.GetString(4) + "~" +
rdr.GetString(5) + "~" };
ga.Add(gi);
}

string[][] g = (string[][]) ga.ToArray(typeof(string[]));

Ken
"Mike" <no****@nospam.com> wrote in message news:uQ*************@TK2MSFTNGP12.phx.gbl...

Hi,

How can I declare and instantiate a multidimensional array that I need to return after having populated it from a DB. Here is the code:

string[,] g = new string[2,];
int i = 0;
while(rdr.Read())
{
g[0, i] = rdr.GetString(1) + " " + rdr.GetString(2);
g[1, i] = Convert.ToString(rdr.GetInt32(0)) + "~" +
rdr.GetString(3) + "~" +
rdr.GetString(4) + "~" +
rdr.GetString(5) + "~";
i++;
}

What is wrong with the above code? I try everything...

Thanks.
Mike
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
4
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.