How to redim an arry ?

G

Gilles Nambert

Hi,

I come form VB and i'm used to Redim statement to modifiy the size of an
arry dunamically, keeping his content, and i dont't find how to do this
simple operation in C# ?

Thanks and best regards
Gilles
 
G

Guest

Hi Gilles,

Just do a reinitialization.

Eg:
int[] myArray = new int[5];
myArray = new int[7];


HTH,
Rakesh Rajan
 
J

Jon Skeet [C# MVP]

Gilles Nambert said:
I come form VB and i'm used to Redim statement to modifiy the size of an
arry dunamically, keeping his content, and i dont't find how to do this
simple operation in C# ?

Create a new array, copy the contents of the old one to the new one
(with Array.Copy) and then set the value of the old variable to a
reference to the new array.
 
L

Lasse Vågsæther Karlsen

Hi,

I come form VB and i'm used to Redim statement to modifiy the size of an
arry dunamically, keeping his content, and i dont't find how to do this
simple operation in C# ?

This is a generic solution, no doubt more specialized solutions can be made
easily:

http://www.vkarlsen.no/ReDim.cs

Example of usage:

Int32[] i = new Int32[] { 1, 2, 3 };
Debug.WriteLine("i has " + i.Length.ToString() + " elements");
i = (Int32[])ClassName.ReDim(i, 10);
Debug.WriteLine("i has " + i.Length.ToString() + " elements");

make sure you alter "ClassName" in the documentation and examples to the
name of the class where you place this, if you intend to use it.
 
G

Guest

Hi Gilles,

Oops...sorry, i though you just wanted to reinitialze an array.

For this case, you will have to use Array.Copy
http://msdn.microsoft.com/library/d...pref/html/frlrfsystemarrayclasscopytopic1.asp

Eg:
Array.Copy(firstArray, secondArray, firstArray.Length)

HTH,
Rakesh Rajan

Rakesh Rajan said:
Hi Gilles,

Just do a reinitialization.

Eg:
int[] myArray = new int[5];
myArray = new int[7];


HTH,
Rakesh Rajan

Gilles Nambert said:
Hi,

I come form VB and i'm used to Redim statement to modifiy the size of an
arry dunamically, keeping his content, and i dont't find how to do this
simple operation in C# ?

Thanks and best regards
Gilles
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

An array cannot be dinamically expanded, you could just create a new one as
the other posts suggest.

My advise is to use ArrayList if you can, it's the more close thing to a VB6
array.

Cheers,
 
D

David Anton

As others have indicated, ArrayList may be a smarter choice.

The exact C# equivalent is a little messy. From our Instant C# VB.NET
to C# converter, assuming an integer array:

VB:
ReDim Preserve x(3)

C#:
int[] Temp1 = new int[4];
if (x != null)
System.Array.Copy(x, Temp1, System.Math.Min(x.Length,
Temp1.Length));
x = Temp1;

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
G

Gilles Nambert

Ok,

I Know it's not possible and how to proceed with a copy or using an
ArrayList.

Thanks
Gilles
 

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