473,385 Members | 1,912 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,385 software developers and data experts.

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 21 '05 #1
6 4210
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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.