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