469,336 Members | 5,286 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

C# equivalent of VB.NET Redim Preserve

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).
Nov 17 '05 #1
6 32496
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

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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).

Nov 17 '05 #2
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

"John Grandy" wrote:
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).

Nov 17 '05 #3
Nope. The closest thing is using the ArrayList which supports an Add()
method allowing you to increase capacity without redimensioning.

HTH,
Joe
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #4
John,

You would not want to use it in both of the languages.
Use as already told by others when it is a simple array an arraylist and in
all other cases an array/collection that implements ilist or icollection.
You can create one as well easy yourself using collectionbase.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 17 '05 #5
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 ?

"Cor Ligthert" <no************@planet.nl> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
John,

You would not want to use it in both of the languages.
Use as already told by others when it is a simple array an arraylist and
in all other cases an array/collection that implements ilist or
icollection. You can create one as well easy yourself using
collectionbase.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor

Nov 17 '05 #6
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

"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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 ?

"Cor Ligthert" <no************@planet.nl> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
John,

You would not want to use it in both of the languages.
Use as already told by others when it is a simple array an arraylist and
in all other cases an array/collection that implements ilist or
icollection. You can create one as well easy yourself using
collectionbase.

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor


Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Wayne Wengert | last post: by
4 posts views Thread by jamie | last post: by
1 post views Thread by Freddy Coal | last post: by
1 post views Thread by keyser soze | last post: by
2 posts views Thread by eBob.com | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.