PC Review


Reply
Thread Tools Rate Thread

C# equivalent of VB.NET Redim Preserve

 
 
John Grandy
Guest
Posts: n/a
 
      24th Mar 2005
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).


 
Reply With Quote
 
 
 
 
Bob Grommes
Guest
Posts: n/a
 
      24th Mar 2005
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$(E-Mail Removed)...
> 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).
>



 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      24th Mar 2005
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).
>
>
>

 
Reply With Quote
 
CometJoe
Guest
Posts: n/a
 
      24th Mar 2005
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
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      24th Mar 2005
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


 
Reply With Quote
 
John Grandy
Guest
Posts: n/a
 
      24th Mar 2005
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" <(E-Mail Removed)> wrote in message
news:O%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Bob Grommes
Guest
Posts: n/a
 
      24th Mar 2005
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:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:O%(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to code ReDim Preserve when "ReDim strArr(1 To 100, 1 To 3)" EagleOne@discussions.microsoft.com Microsoft Excel Programming 10 11th May 2009 04:47 PM
Redim preserve does not work for me Dan Microsoft Excel Programming 4 18th Mar 2009 12:01 PM
Redim Preserve question Sam Kuo Microsoft Excel Programming 11 3rd Jun 2008 06:38 AM
redim preserve RobcPettit@yahoo.co.uk Microsoft Excel Programming 3 15th Dec 2005 01:40 PM
Redim Preserve Array LP Microsoft VB .NET 6 11th Feb 2005 07:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.