473,399 Members | 3,919 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,399 software developers and data experts.

how to increase the size of array and preserve the previous data?

Tee
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
Nov 16 '05 #1
8 10582
You didn't specify what type of array but for a byte array you could use the
following method:

public byte [] ReDim(byte [] array, int newSize)

{

byte [] abytNew = new byte[newSize];

Buffer.BlockCopy(array, 0, abytNew, 0, newSize);

return abytNew;

}

Buffer.BlockCopy can handle any type inherited from Array:

byte []
int []
long []

etc...
HTH

"Tee" <th*@streamyx.com> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
Tee
It will be a string array.

Thanks,
Tee

"mooni" <to*****@hotmail.com> wrote in message
news:41********@dnews.tpgi.com.au...
You didn't specify what type of array but for a byte array you could use the following method:

public byte [] ReDim(byte [] array, int newSize)

{

byte [] abytNew = new byte[newSize];

Buffer.BlockCopy(array, 0, abytNew, 0, newSize);

return abytNew;

}

Buffer.BlockCopy can handle any type inherited from Array:

byte []
int []
long []

etc...
HTH

"Tee" <th*@streamyx.com> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #3
Buffer.BlockCopy also applies to string arrays:

public string [] ReDim(string [] array, int newSize)
{
string [] astrNew = new string[newSize];
Buffer.BlockCopy(array, 0, astrNew, 0, newSize);
return astrNew;
}

HTH
mooni

"Tee" <th*@streamyx.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
It will be a string array.

Thanks,
Tee

"mooni" <to*****@hotmail.com> wrote in message
news:41********@dnews.tpgi.com.au...
You didn't specify what type of array but for a byte array you could use

the
following method:

public byte [] ReDim(byte [] array, int newSize)

{

byte [] abytNew = new byte[newSize];

Buffer.BlockCopy(array, 0, abytNew, 0, newSize);

return abytNew;

}

Buffer.BlockCopy can handle any type inherited from Array:

byte []
int []
long []

etc...
HTH

"Tee" <th*@streamyx.com> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 16 '05 #4
Tee
It gives error:

An unhandled exception of type 'System.ArgumentException' occurred in
WindowsApplication10.exe

Additional information: Object must be an array of primitives.

if I comment out this Buffer.BlockCopy, it will work, but can't preserve the
old value.
Thanks.

"mooni" <to*****@hotmail.com> wrote in message
news:41******@dnews.tpgi.com.au...
Buffer.BlockCopy also applies to string arrays:

public string [] ReDim(string [] array, int newSize)
{
string [] astrNew = new string[newSize];
Buffer.BlockCopy(array, 0, astrNew, 0, newSize);
return astrNew;
}

HTH
mooni

"Tee" <th*@streamyx.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
It will be a string array.

Thanks,
Tee

"mooni" <to*****@hotmail.com> wrote in message
news:41********@dnews.tpgi.com.au...
You didn't specify what type of array but for a byte array you could
use the
following method:

public byte [] ReDim(byte [] array, int newSize)

{

byte [] abytNew = new byte[newSize];

Buffer.BlockCopy(array, 0, abytNew, 0, newSize);

return abytNew;

}

Buffer.BlockCopy can handle any type inherited from Array:

byte []
int []
long []

etc...
HTH

"Tee" <th*@streamyx.com> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 16 '05 #5
My mistake, string [] doesn't inherit from System.Array

Try:

public string [] ReDim(string [] array, int newSize)

{

string [] abytNew = new string[newSize];

for(int iCounter = 0; iCounter < array.Length; iCounter++)

abytNew[iCounter] = array[iCounter];

return abytNew;

}
HTH
mooni

"Tee" <th*@streamyx.com> wrote in message
news:O0**************@TK2MSFTNGP14.phx.gbl...
It gives error:

An unhandled exception of type 'System.ArgumentException' occurred in
WindowsApplication10.exe

Additional information: Object must be an array of primitives.

if I comment out this Buffer.BlockCopy, it will work, but can't preserve
the
old value.
Thanks.

"mooni" <to*****@hotmail.com> wrote in message
news:41******@dnews.tpgi.com.au...
Buffer.BlockCopy also applies to string arrays:

public string [] ReDim(string [] array, int newSize)
{
string [] astrNew = new string[newSize];
Buffer.BlockCopy(array, 0, astrNew, 0, newSize);
return astrNew;
}

HTH
mooni

"Tee" <th*@streamyx.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> It will be a string array.
>
> Thanks,
> Tee
>
> "mooni" <to*****@hotmail.com> wrote in message
> news:41********@dnews.tpgi.com.au...
>> You didn't specify what type of array but for a byte array you could use > the
>> following method:
>>
>> public byte [] ReDim(byte [] array, int newSize)
>>
>> {
>>
>> byte [] abytNew = new byte[newSize];
>>
>> Buffer.BlockCopy(array, 0, abytNew, 0, newSize);
>>
>> return abytNew;
>>
>> }
>>
>> Buffer.BlockCopy can handle any type inherited from Array:
>>
>> byte []
>> int []
>> long []
>>
>> etc...
>>
>>
>> HTH
>>
>> "Tee" <th*@streamyx.com> wrote in message
>> news:eD**************@TK2MSFTNGP10.phx.gbl...
>> > 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
>> >
>> >
>>
>>
>
>



Nov 16 '05 #6
Tee
I got it to work after I change to Buffer.BlockCopy code to:

System.Array.Copy(array, astrNew, array.Length);

Thanks,

Tee
"Tee" <th*@streamyx.com> wrote in message
news:O0**************@TK2MSFTNGP14.phx.gbl...
It gives error:

An unhandled exception of type 'System.ArgumentException' occurred in
WindowsApplication10.exe

Additional information: Object must be an array of primitives.

if I comment out this Buffer.BlockCopy, it will work, but can't preserve the old value.
Thanks.

"mooni" <to*****@hotmail.com> wrote in message
news:41******@dnews.tpgi.com.au...
Buffer.BlockCopy also applies to string arrays:

public string [] ReDim(string [] array, int newSize)
{
string [] astrNew = new string[newSize];
Buffer.BlockCopy(array, 0, astrNew, 0, newSize);
return astrNew;
}

HTH
mooni

"Tee" <th*@streamyx.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
It will be a string array.

Thanks,
Tee

"mooni" <to*****@hotmail.com> wrote in message
news:41********@dnews.tpgi.com.au...
> You didn't specify what type of array but for a byte array you could use the
> following method:
>
> public byte [] ReDim(byte [] array, int newSize)
>
> {
>
> byte [] abytNew = new byte[newSize];
>
> Buffer.BlockCopy(array, 0, abytNew, 0, newSize);
>
> return abytNew;
>
> }
>
> Buffer.BlockCopy can handle any type inherited from Array:
>
> byte []
> int []
> long []
>
> etc...
>
>
> HTH
>
> "Tee" <th*@streamyx.com> wrote in message
> news:eD**************@TK2MSFTNGP10.phx.gbl...
> > 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
> >
> >
>
>



Nov 16 '05 #7
mooni <to*****@hotmail.com> wrote:
My mistake, string [] doesn't inherit from System.Array


Um, yes it does...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Could you give a broader statement of your original problem? What are
you trying to do? ArrayList is quite capable of supporting
multi-dimensional arrays, but it all depends upon what you want to do
with the arrays and how you want them to work.

Nov 16 '05 #9

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

Similar topics

0
by: Jawahar Rajan | last post by:
All, I have a datagrid that I use in a ASP.net web page that loads customer data based on the selected customer ID Some Customers will have no data in the database (which is oracle 9.2) . So I...
2
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
3
by: Tee | last post by:
Hi, Anyone know how to redeclare an array with different size but keep the previous data? Just like the redim preserve in VB. Thanks.
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
2
by: yoshitha | last post by:
Hi Can any one tell me how to increase size of an array in C#.net with ASP.Net? its very urgent for me thanks yoshitha
5
by: partha.p.das | last post by:
Here is my class: class MyClass { public: MyClass(); ~MyClass(); private:
23
by: Bjorn | last post by:
Hi. Every time i post data in a form the contents are being checked for validity. When i click the back-button, all data is gone and i have to retype it. It's obvious that only a few or none of...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
1
by: nagmvs | last post by:
Hello I am unable to upload More than 200kb in to my server and also i can downlaod only less than 4MB file from my server .When i am searching in internet i found solution as u can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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
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
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
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.