Redim Preserve Array

L

LP

Hello,

What is C# equivalent of rediming an array and preserving exciting elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)
Thank you
 
N

Nicholas Paldino [.NET/C# MVP]

LP,

You will want to use an ArrayList, which can be found in the
System.Collections namespace. When it returns a value, however, you will
have to cast that value to the type that is stored in it.

In .NET 2.0, you would use a List<T> instance (where T is the type you
want to store in the array), which can be found in the
System.Collections.Generic namespace.

Hope this helps.
 
H

Herfried K. Wagner [MVP]

LP said:
What is C# equivalent of rediming an array and preserving exciting
elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)

First, create a new array of the desired size. Then use 'Array.Copy' to
copy the contents of the "old" array to the new array. After doing that,
assign the new array to the variable that is currently referencing the old
array.

If the number of items in an array changes frequently, consider using a more
dynamic datastructure, like an arraylist or one of the other collections
available in the 'System.Collections' namespace.
 
U

Urs Vogel

LP

There's nothing like it in C#, you have to build it own your own, if you
want to use plain arrays. But why don't you use the ArrayList class?

Urs
 
L

LP

Thank you all,

I would like to use ArrayList, but I am getting an array of strings from a
VB.NET class that someone else developed. I need to do further manipulation
on this array, add more elements and then pass it to another object as Array
of strings again that will store its data to SQL db.
I don't know if it makes sense to convert this Array to ArrayList and then
back to another Array.
The best option at this point (besides rewriting 2 objects) is to use
Array.Copy. Any other ideas?
 
N

Nicholas Paldino [.NET/C# MVP]

LP,

The best choice is to use the ArrayList class, do the work you need to
do, and then get the array back by using the ToArray method on the ArrayList
class.
 
L

LP

Yes, that would the best choice, thank you!

Nicholas Paldino said:
LP,

The best choice is to use the ArrayList class, do the work you need to
do, and then get the array back by using the ToArray method on the ArrayList
class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

LP said:
Thank you all,

I would like to use ArrayList, but I am getting an array of strings from a
VB.NET class that someone else developed. I need to do further
manipulation
on this array, add more elements and then pass it to another object as
Array
of strings again that will store its data to SQL db.
I don't know if it makes sense to convert this Array to ArrayList and then
back to another Array.
The best option at this point (besides rewriting 2 objects) is to use
Array.Copy. Any other ideas?
 

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