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

C# counterpart to VB.NET Redim

Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas
Feb 10 '06 #1
4 1717
"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas


You need to reallocate and copy yourself. To can use the array's Copy(To)
method for this purpose.

If you don't want to do this, just use an ArrayList instead of an array and
just Add() elements all you want and let the collection take care of things
for you.

m
Feb 10 '06 #2
Additionally, in .NET 2.0, you can just use the Generic type List<T>
which will do this as well and give you type safety.

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

"Mike" <vi********@yahoo.com> wrote in message
news:Og**************@TK2MSFTNGP15.phx.gbl...
"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas


You need to reallocate and copy yourself. To can use the array's Copy(To)
method for this purpose.

If you don't want to do this, just use an ArrayList instead of an array
and just Add() elements all you want and let the collection take care of
things for you.

m

Feb 10 '06 #3
First of all, a .NET collection such as ArrayList might be preferable.
However, if you want to resize an array, then the following is what you need:
(assuming an array of type "SomeType")

SomeType[] temp = new SomeType[11];
if (A != null)
Array.Copy(A, temp, Math.Min(A.Length, temp.Length));
A = temp;
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Niklas" wrote:
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas

Feb 11 '06 #4
List<T> is dynamic, so you don't even need to redim.

--
William Stacey [MVP]

"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
| Hi
| What is the best way to enlarge an C# array which already have values?
| In VB.NET I would do
| ReDim Preserve A(10)
| regards
| /Niklas
Feb 11 '06 #5

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

Similar topics

4
by: Trevor Fairchild | last post by:
I've got a program that parses text files. The text files come to me in Unicode and they contain goofy characters that VB chokes on - treats them as eof markers. I have already been through this...
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: Daryl Davis | last post by:
I am having trouble with ReDim (see code below) SaleTable2's structure includes an array for SaleDetailTable2 Dim newsale As New hallsales.SaleTable2 Dim detail As New hallsales.SaleDetailTable2...
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...
9
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
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...
4
by: Han | last post by:
Hello I am passing string from dotnet to javascript. ", ', newline characters, and something. So I am using httputility.urlEncode() to make it neat. Now a JS function accepts the encoded string...
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: 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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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
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...

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.