473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create 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 6600
LP,

You will want to use an ArrayList, which can be found in the
System.Collections 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.Collections.Generic namespace.

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

"LP" <lp@a.com> wrote in message
news:eS**************@TK2MSFTNGP12.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.Collections' 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**************@TK2MSFTNGP12.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*************@TK2MSFTNGP15.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**************@TK2MSFTNGP12.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.com

"LP" <lp@a.com> wrote in message
news:er**************@tk2msftngp13.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*************@TK2MSFTNGP15.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**************@TK2MSFTNGP12.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.com> wrote in
message news:eZ**************@TK2MSFTNGP14.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.com

"LP" <lp@a.com> wrote in message
news:er**************@tk2msftngp13.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*************@TK2MSFTNGP15.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**************@TK2MSFTNGP12.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
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...
7
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...
6
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...
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: 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...
0
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,...
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...
1
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
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...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.