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

T

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
 
M

mooni

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
 
T

Tee

It will be a string array.

Thanks,
Tee

mooni said:
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 said:
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
 
M

mooni

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 said:
It will be a string array.

Thanks,
Tee

mooni said:
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 said:
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
 
T

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 said:
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 said:
It will be a string array.

Thanks,
Tee

mooni said:
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

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
 
M

mooni

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 said:
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 said:
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 said:
It will be a string array.

Thanks,
Tee

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

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
 
T

Tee

I got it to work after I change to Buffer.BlockCopy code to:

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



Thanks,

Tee


Tee said:
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 said:
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 said:
It will be a string array.

Thanks,
Tee

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

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
 
B

Bruce Wood

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top