473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 33059
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wayne Wengert | last post by:
I am trying to add one column to an existing array (code below). The ReDim command gives the error: ----------------------------------------------- Microsoft VBScript runtime error '800a0009' ...
2
by: | last post by:
Is it correct to think that after reducing the populated array's size from say, 10 to 5 with redim preserve myArray(i) an attempt to access an element above the fifth does not cause a...
4
by: jamie | last post by:
As you might have guessed, I am trying to switch from vb to c#, but I am having a little difficulty. How would you resize an array in c# ? Is there a "redim preserve" as well?? An example...
5
by: Zenobia | last post by:
Hello, I want to keep a list references to database records being accessed. I will do this by storing the record keys in a list. The list must not contain duplicate keys. So I check the...
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
1
by: Freddy Coal | last post by:
Hi, I don't know how redim an array, My problem whit an example: I define my array Dim Ary as array I put three elements inside my array Ary = Split("one,two,three", ",")
1
by: keyser soze | last post by:
hi REDIM Preserve reports an "out of range" i first create an array, store it into a session var then, in other page, i load restore the session var into a local array but, after this, i can't...
2
by: eBob.com | last post by:
I was changing some code in a multi-threaded application today and noticed that it was not locking where it really needed to be locking. The Sub was already working with an array so I just stuck a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.