int [] Array Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Galahad said:
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.
 
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 said:
Galahad said:
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.
 
Galahad said:
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 said:
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.
 
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 ([email protected])



Galahad said:
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 said:
Galahad said:
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.
 
Spectre said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top