473,799 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 33120
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$******** ******@TK2MSFTN GP12.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(newsi ze)

is the following in C#:

int[] tempReDim = new int[newsize + 1];
if (thisArray != null)
System.Array.Co py(thisArray, tempReDim,
System.Math.Min (thisArray.Leng th, tempReDim.Lengt h));
thisArray = tempReDim1;

Not very pleasant - you might want to look at some of the collections in the
System.Collecti ons 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%******** ********@TK2MSF TNGP12.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<M yClass> aList = new SizeableArray<M yClass>(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******** ********@TK2MSF TNGP15.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%******** ********@TK2MSF TNGP12.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
15892
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' Subscript out of range /ListCGShowsGrouped.asp, line 58 ------------------------------------------------
2
4756
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 compillation error "array out of script", but returns whatever heppened to be written in that memory address (in particular it might return the correct values of those elements before re-dimentioning)? This seems to be the case in my code, yet I wanted to...
4
1433
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 would go a long way.
5
2187
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 Contains property before adding. Question. Can I use an Array for this or should I use an
5
6910
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 Preserve SomeArray(c) SomeArray(c) = SomeObject <end loop>
19
3155
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
1
1883
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
2295
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 REDIM Preserve thanks ks
2
2942
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 SyncLock ArrayName.SyncRoot at the beginning of the Sub and an End SyncLock at the end. But this caused the application to produce no output (an Excel spreadsheet)! After some screwing around, sorry ... I mean experimenting, I noticed that the...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.