473,397 Members | 2,068 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,397 software developers and data experts.

preserve dynamic array's old data

when we redefine an array ,how can we preserve the old data?

string[] a = new string[1];
a[1] = "old data";

when we redefine the array a as following:
a = new string[2];

the data of a[1] was lost

what can i do to prevent it?

thanks a lot

Nov 16 '05 #1
3 3944
Create another variable to hold the new array, and copy the old array's
contents. In VB.NET, you could use Preserve keyword.

string[] a = new string[5];
string[] b = new string[10];

Array.Copy(a, b, 5);

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"watcher" <ti**********@tom.com> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
when we redefine an array ,how can we preserve the old data?

string[] a = new string[1];
a[1] = "old data";

when we redefine the array a as following:
a = new string[2];

the data of a[1] was lost

what can i do to prevent it?

thanks a lot


Nov 16 '05 #2

"watcher" <ti**********@tom.com> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
when we redefine an array ,how can we preserve the old data?

string[] a = new string[1];
a[1] = "old data";

when we redefine the array a as following:
a = new string[2];

the data of a[1] was lost

what can i do to prevent it?


You would be best doing something like...

string[] b = new string[2];
Array.Copy(a,b,a.Length);
a = b;

It seems to me there is a resize method hiding somewhre in the framework as
well...but I can't recall what it is anylonger.
Nov 16 '05 #3
Maybe try using the ArrayList class instead? It will handle that for you.

--
Adam Clauss
ca*****@tamu.edu

"watcher" <ti**********@tom.com> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
when we redefine an array ,how can we preserve the old data?

string[] a = new string[1];
a[1] = "old data";

when we redefine the array a as following:
a = new string[2];

the data of a[1] was lost

what can i do to prevent it?

thanks a lot


Nov 16 '05 #4

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

Similar topics

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...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
8
by: Tee | last post by:
Hi, How do we increase the size of array on runtime and preserve the previous data? I don't want to use ArrayList because the array could be a multi-dimension array. Thanks, Tee
6
by: LP | last post by:
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
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: 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...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
2
by: doomer | last post by:
Hello, I'm trying to build a set of classes that act as the handlers for a simple expression parser, and i'm wondering how to preserve the dynamic type information from derived classes. An...
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: 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
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
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,...
0
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...

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.