redim arrayx?????

J

jamie

As you might have guessed,
I am trying to switch from
vb to c#, but I am having
a little difficulty.

How would you resize an array in
c# ? Is there a "redim preserve"
as well??

An example would go a long way.
Thanks jamie
 
B

Bruce Wood

You have to do it yourself, manually. You have to create a new array
and copy the elements into it:

int[] myArray = new int[5];
int[] temp = new int[10];
for (int i = 0; i < myArray.Length; i++)
{
temp = myArray;
}
myArray = temp;

That's why it's much more common in C# to use ArrayList and convert to
an array only when you really need an array (for example to pass to
some method that requires an array).
 
D

Dale Preston

Use an ArrayList. It will resize itself as you need it to. Internally, it
creates a copy of the array with the new size, and copies the existing data
to the copy, then renames the copy - which is exactly what a redim preserve
in VB does internally.

HTH

Dale Preston
MCAD, MCDBA, MCSE
 
J

jamie

I think the recipe is exactly
what I need to do. I knew I could
do it that way, I was just hoping
that I wouldnt have to for clarity
sake.

Thanks.
 

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

Top