473,799 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3968
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**********@t om.com> wrote in message
news:O8******** ******@TK2MSFTN GP14.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**********@t om.com> wrote in message
news:O8******** ******@TK2MSFTN GP14.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.ed u

"watcher" <ti**********@t om.com> wrote in message
news:O8******** ******@TK2MSFTN GP14.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
4756
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...
5
3410
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 be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
8
3688
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
8
10607
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
6625
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
33120
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 changed).
5
6910
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>
60
10211
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: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is readily >implemented in template classes that can create dynamically allocated >multi-dimensional...
2
1774
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 example will help illustrate what I have in mind... class Numeric { // contains discriminated union to hold data and identify data
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10475
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...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
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
6805
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
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.