C# equivalent of VB.NET Redim Preserve

J

John Grandy

Does C# have an equivalent for VB.NET's Redim Preserve ?

ReDim Preserve increases the final dimension of any array while preserving
the array's contents (however, the type of the array may not be changed).
 
B

Bob Grommes

No, and .NET arrays don't have this ability either, so all the VB.NET
implementation of ReDim Preserve does is what you'd have to do in C# --
create a new array of the desired size and use Array.Copy to populate it
from the original array, then destroy the original.

Something closer to dynamic arrays is the ArrayList, but it has the
disadvantage of boxing / unboxing to deal with. In VS 2005, the generic
class List<T> does away with this, and fills the office of a strongly-typed
dynamically sizable array -- albiet with somewhat different syntax than a
conventional array, but that can be solved very easily with a wrapper class.

--Bob
 
G

Guest

In case you're wondering, the closest thing to this simple redim preserve:

ReDim Preserve thisArray(newsize)

is the following in C#:

int[] tempReDim = new int[newsize + 1];
if (thisArray != null)
System.Array.Copy(thisArray, tempReDim,
System.Math.Min(thisArray.Length, tempReDim.Length));
thisArray = tempReDim1;

Not very pleasant - you might want to look at some of the collections in the
System.Collections namespace.

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to
VB.NET converter
 
C

CometJoe

Nope. The closest thing is using the ArrayList which supports an Add()
method allowing you to increase capacity without redimensioning.

HTH,
Joe
 
J

John Grandy

Hi Cor, and thanks for the response.

How do you define "simple array". For example, would it be wise to store
instances of my custom class in an ArrayList ?
 
B

Bob Grommes

You can store anything in an ArrayList and this is common practice where a
single-dimensioned collection that only needs to be addressed via numeric
index is appropriate. If the number of items is known and development time,
an array is more performant, but if you need dynamic sizing, an ArrayList is
the way to go.

ArrayList aList = new ArrayList();
aList.Add(new MyClass()); // "element" 0
aList.Add(new MyClass()); // "element" 1
MyClass myClassInstance = (MyClass)aList[0]; // retreive element 0
MyClass myOtherInstance = (MyClass)aList[1]; // retreive element 1

If you want to avoid the casting in your client code, derive a typed
collection from CollectionBase -- it will work about the same except that
the items in the collection will be of your specific type rather than simply
System.Object.

As I mentioned before, with CLR 2.0 / VS 2005 you can use List<T> to make it
more performant and type-safe:

List<MyClass> aList = new List<MyClass>();
aList.Add(new MyClass());
aList.Add(new MyClass());
MyClass myClassInstance = aList[0];
MyClass myOtherInstance = aList[1];

.... and so forth. This client code storage and retreival logic looks the
same as a CLR 1.1 implementation derived from CollectionBase, but the latter
just *hides* the casting, whereas a generic List<T> actually *is* of your
custom type.

If you want more of an array syntax you could derive a wrapper class from
ArrayList or List<T> and then you could pretend it was a resizeable array:

SizeableArray<MyClass> aList = new SizeableArray<MyClass>(2);
aList[0] = new MyClass();
aList[1] = new MyClass();
aList.Resize(3);
aList[2] = new MyClass();
// etc.

--Bob
 

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

Similar Threads


Top