473,699 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1727
"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.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.co m

"Mike" <vi********@yah oo.com> wrote in message
news:Og******** ******@TK2MSFTN GP15.phx.gbl...
"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.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.Leng th, 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****@discuss ions.microsoft. com> wrote in message
news:9D******** *************** ***********@mic rosoft.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
7977
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 issue in another thread and I have received an answer that works (code is below) However, my test data thus far has been in the 10-50MB size range. I now have a case where the text file is 650MB in size! I need to convert it to ANSI for my...
2
15889
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
4746
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
1660
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 Try redim newsale.SaleDetails(0) ********* This line comes out with an error "Redim statement requires Array" *************
5
2184
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
9
5699
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
6907
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
3149
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
4
7187
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 and decode using decodeURI(). But still there are unsolved characters and some +, %, and hex numbers. What is exact dotnet counterpart of JS decodeURI()? I am not sure this matters. I am not using typical asp.net + JS. I am using...
0
8630
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
9199
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
8944
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
8899
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...
0
7786
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...
0
5889
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
4391
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
3075
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
2364
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.