473,808 Members | 2,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Redim Preserve Array

LP
Hello,

What is C# equivalent of rediming an array and preserving exciting elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)
Thank you
Nov 16 '05 #1
6 6626
LP,

You will want to use an ArrayList, which can be found in the
System.Collecti ons namespace. When it returns a value, however, you will
have to cast that value to the type that is stored in it.

In .NET 2.0, you would use a List<T> instance (where T is the type you
want to store in the array), which can be found in the
System.Collecti ons.Generic namespace.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"LP" <lp@a.com> wrote in message
news:eS******** ******@TK2MSFTN GP12.phx.gbl...
Hello,

What is C# equivalent of rediming an array and preserving exciting
elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)
Thank you

Nov 16 '05 #2
"LP" <lp@a.com> schrieb:
What is C# equivalent of rediming an array and preserving exciting
elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)


First, create a new array of the desired size. Then use 'Array.Copy' to
copy the contents of the "old" array to the new array. After doing that,
assign the new array to the variable that is currently referencing the old
array.

If the number of items in an array changes frequently, consider using a more
dynamic datastructure, like an arraylist or one of the other collections
available in the 'System.Collect ions' namespace.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 16 '05 #3
LP

There's nothing like it in C#, you have to build it own your own, if you
want to use plain arrays. But why don't you use the ArrayList class?

Urs

"LP" <lp@a.com> schrieb im Newsbeitrag
news:eS******** ******@TK2MSFTN GP12.phx.gbl...
Hello,

What is C# equivalent of rediming an array and preserving exciting
elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)
Thank you

Nov 16 '05 #4
LP
Thank you all,

I would like to use ArrayList, but I am getting an array of strings from a
VB.NET class that someone else developed. I need to do further manipulation
on this array, add more elements and then pass it to another object as Array
of strings again that will store its data to SQL db.
I don't know if it makes sense to convert this Array to ArrayList and then
back to another Array.
The best option at this point (besides rewriting 2 objects) is to use
Array.Copy. Any other ideas?

"Urs Vogel" <uv****@msn.com > wrote in message
news:OT******** *****@TK2MSFTNG P15.phx.gbl...
LP

There's nothing like it in C#, you have to build it own your own, if you
want to use plain arrays. But why don't you use the ArrayList class?

Urs

"LP" <lp@a.com> schrieb im Newsbeitrag
news:eS******** ******@TK2MSFTN GP12.phx.gbl...
Hello,

What is C# equivalent of rediming an array and preserving exciting
elements.
VB.NET syntax looks something like this:
ReDim Preserve myArray(5)
Thank you


Nov 16 '05 #5
LP,

The best choice is to use the ArrayList class, do the work you need to
do, and then get the array back by using the ToArray method on the ArrayList
class.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"LP" <lp@a.com> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
Thank you all,

I would like to use ArrayList, but I am getting an array of strings from a
VB.NET class that someone else developed. I need to do further
manipulation
on this array, add more elements and then pass it to another object as
Array
of strings again that will store its data to SQL db.
I don't know if it makes sense to convert this Array to ArrayList and then
back to another Array.
The best option at this point (besides rewriting 2 objects) is to use
Array.Copy. Any other ideas?

"Urs Vogel" <uv****@msn.com > wrote in message
news:OT******** *****@TK2MSFTNG P15.phx.gbl...
LP

There's nothing like it in C#, you have to build it own your own, if you
want to use plain arrays. But why don't you use the ArrayList class?

Urs

"LP" <lp@a.com> schrieb im Newsbeitrag
news:eS******** ******@TK2MSFTN GP12.phx.gbl...
> Hello,
>
> What is C# equivalent of rediming an array and preserving exciting
> elements.
> VB.NET syntax looks something like this:
> ReDim Preserve myArray(5)
> Thank you
>
>



Nov 16 '05 #6
LP
Yes, that would the best choice, thank you!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eZ******** ******@TK2MSFTN GP14.phx.gbl...
LP,

The best choice is to use the ArrayList class, do the work you need to
do, and then get the array back by using the ToArray method on the ArrayList class.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"LP" <lp@a.com> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
Thank you all,

I would like to use ArrayList, but I am getting an array of strings from a VB.NET class that someone else developed. I need to do further
manipulation
on this array, add more elements and then pass it to another object as
Array
of strings again that will store its data to SQL db.
I don't know if it makes sense to convert this Array to ArrayList and then back to another Array.
The best option at this point (besides rewriting 2 objects) is to use
Array.Copy. Any other ideas?

"Urs Vogel" <uv****@msn.com > wrote in message
news:OT******** *****@TK2MSFTNG P15.phx.gbl...
LP

There's nothing like it in C#, you have to build it own your own, if you want to use plain arrays. But why don't you use the ArrayList class?

Urs

"LP" <lp@a.com> schrieb im Newsbeitrag
news:eS******** ******@TK2MSFTN GP12.phx.gbl...
> Hello,
>
> What is C# equivalent of rediming an array and preserving exciting
> elements.
> VB.NET syntax looks something like this:
> ReDim Preserve myArray(5)
> Thank you
>
>



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15893
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
4757
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...
7
14657
by: mb | last post by:
I have an array that starts with 99 index. I use a random amount of the indexes, let's say 23. Is there a way to truncate the array to 23 elements while preserving the 23 used? The ReDim statement in VB does this. Is there an equivalent in C#?
6
33123
by: John Grandy | last post by:
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).
5
2188
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
6911
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
3159
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
1885
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
2297
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
2943
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
9721
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
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
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
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
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
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
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
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.