473,396 Members | 1,995 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,396 software developers and data experts.

int [] Array Question

I am using the code below to create a integer array.

while(dr.Read())
{
string StateID=dr["StateID"].ToString();
int FacilityID=Convert.ToInt32(dr["FacilityID"].ToString());
int [] FacilityIDs={FacilityID};
}

However, the last line int [] FacilityIDs={FacilityID}; only adds on value
to the array not all of the values.

What am I doing wrong?

Thanks
Mar 27 '06 #1
5 1716
Galahad <Ga*****@discussions.microsoft.com> wrote:
I am using the code below to create a integer array.

while(dr.Read())
{
string StateID=dr["StateID"].ToString();
int FacilityID=Convert.ToInt32(dr["FacilityID"].ToString());
int [] FacilityIDs={FacilityID};
}

However, the last line int [] FacilityIDs={FacilityID}; only adds on value
to the array not all of the values.

What am I doing wrong?


You're creating an array each time you go through the loop. Assuming
you don't know how many rows you've actually got, I'd suggest using an
ArrayList (if you're using .NET 1.1) or a List<int> (if you're using
..NET 2.0). Create the list outside the loop, then add to it inside the
loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 27 '06 #2
Ok that makes sense. I can't use an arraylist since the function only takes
int[] as a param.

What is the syntax that I would use inside the loop to add the elements?

int [] FacilityIDs;

while(dr.Read())
{
string StateID = dr["StateID"].ToString();
int FacilityID = Convert.ToInt32(dr["FacilityID"].ToString());
Add values to array...
}
"Jon Skeet [C# MVP]" wrote:
Galahad <Ga*****@discussions.microsoft.com> wrote:
I am using the code below to create a integer array.

while(dr.Read())
{
string StateID=dr["StateID"].ToString();
int FacilityID=Convert.ToInt32(dr["FacilityID"].ToString());
int [] FacilityIDs={FacilityID};
}

However, the last line int [] FacilityIDs={FacilityID}; only adds on value
to the array not all of the values.

What am I doing wrong?


You're creating an array each time you go through the loop. Assuming
you don't know how many rows you've actually got, I'd suggest using an
ArrayList (if you're using .NET 1.1) or a List<int> (if you're using
..NET 2.0). Create the list outside the loop, then add to it inside the
loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 27 '06 #3
Galahad <Ga*****@discussions.microsoft.com> wrote:
Ok that makes sense. I can't use an arraylist since the function only takes
int[] as a param.
In that case you should use the ToArray method of either ArrayList or
List<int> after you've created the list.
What is the syntax that I would use inside the loop to add the elements?


You can't add values to an array - arrays are fixed size. You'd have to
reallocate the array on each iteration, which is basically what
ArrayList/List<int> do, except they do it more efficiently by keeping a
buffer of unused entries, only resizing when they need to.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 27 '06 #4
int [] FacilityIDs = null;

while(dr.Read())
{
string StateID = dr["StateID"].ToString();
int FacilityID = Convert.ToInt32(dr["FacilityID"].ToString());
AppendToArray(ref FacilityIDs, FacilityID)
}
//....... somewhere later in your code make this new method

void AppendToArray(ref int[] array, int value)
{
if(array == null)
{
array = new int[1];
}
else
{
int[] buffer = new int[array.Length];
array = new int[array.Length + 1];
Array.Copy(buffer, 0, array, 0, buffer.Length);
}
array[array.Length - 1] = value;
}

___________________________

Tell me if this helps

____________________________
Spectre (re*************@hotmail.com)

"Galahad" wrote:
Ok that makes sense. I can't use an arraylist since the function only takes
int[] as a param.

What is the syntax that I would use inside the loop to add the elements?

int [] FacilityIDs;

while(dr.Read())
{
string StateID = dr["StateID"].ToString();
int FacilityID = Convert.ToInt32(dr["FacilityID"].ToString());
Add values to array...
}
"Jon Skeet [C# MVP]" wrote:
Galahad <Ga*****@discussions.microsoft.com> wrote:
I am using the code below to create a integer array.

while(dr.Read())
{
string StateID=dr["StateID"].ToString();
int FacilityID=Convert.ToInt32(dr["FacilityID"].ToString());
int [] FacilityIDs={FacilityID};
}

However, the last line int [] FacilityIDs={FacilityID}; only adds on value
to the array not all of the values.

What am I doing wrong?


You're creating an array each time you go through the loop. Assuming
you don't know how many rows you've actually got, I'd suggest using an
ArrayList (if you're using .NET 1.1) or a List<int> (if you're using
..NET 2.0). Create the list outside the loop, then add to it inside the
loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 29 '06 #5
Spectre <Sp*****@discussions.microsoft.com> wrote:
int [] FacilityIDs = null;

while(dr.Read())
{
string StateID = dr["StateID"].ToString();
int FacilityID = Convert.ToInt32(dr["FacilityID"].ToString());
AppendToArray(ref FacilityIDs, FacilityID)
}
//....... somewhere later in your code make this new method

void AppendToArray(ref int[] array, int value)
{
if(array == null)
{
array = new int[1];
}
else
{
int[] buffer = new int[array.Length];
array = new int[array.Length + 1];
Array.Copy(buffer, 0, array, 0, buffer.Length);
}
array[array.Length - 1] = value;
}


That's a really inefficient way of doing it rather than using an
ArrayList or List<T> outside and then calling ToArray afterwards. Your
way copies the array on every iteration - using List/ArrayList will
only copy it when it runs out of buffer space.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #6

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

Similar topics

3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
8
by: T. Wintershoven | last post by:
Hello all, I have a form with some checkboxes. The names of these checkboxes come from an array. When i click the submit button the resultcode doesn't recognize the names when i want to check...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.