how to redeclare an array with different size but keep the previous data?

T

Tee

Hi,

Anyone know how to redeclare an array with different size but keep the
previous data? Just like the redim preserve in VB.


Thanks.
 
D

Daniel O'Connell [C# MVP]

Tee said:
Hi,

Anyone know how to redeclare an array with different size but keep the
previous data? Just like the redim preserve in VB.

Just create a new array and copy the contents of the old array to it. I
believe thats basically all that ReDim does anyway.
 
J

Jon Skeet [C# MVP]

Tee said:
If the redim is needed in a while loop, how should I do it?

In exactly the same way. However, you can make things faster by only
reallocating size in chunks - for instance, doubling the size of array
each time, and then reallocating it once after the loop to the "right"
size.
 

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