473,382 Members | 1,743 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,382 software developers and data experts.

How to redim an arry ?


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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
6 5721
Hi Gilles,

Just do a reinitialization.

Eg:
int[] myArray = new int[5];
myArray = new int[7];
HTH,
Rakesh Rajan

"Gilles Nambert" wrote:

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Gilles Nambert <gn*****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Gilles Nambert <gn*****@hotmail.com> wrote in news:OEUcrK0tEHA.3932
@TK2MSFTNGP10.phx.gbl:

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.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #4
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/de...copytopic1.asp

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

HTH,
Rakesh Rajan

"Rakesh Rajan" wrote:
Hi Gilles,

Just do a reinitialization.

Eg:
int[] myArray = new int[5];
myArray = new int[7];
HTH,
Rakesh Rajan

"Gilles Nambert" wrote:

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Gilles Nambert" <gn*****@hotmail.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
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
*-----------------------*
Nov 16 '05 #7

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

Similar topics

4
by: Trevor Fairchild | last post by:
I've got a program that parses text files. The text files come to me in Unicode and they contain goofy characters that VB chokes on - treats them as eof markers. I have already been through this...
2
by: Wayne Wengert | last post by:
I am trying to add one column to an existing array (code below). The ReDim command gives the error: ----------------------------------------------- Microsoft VBScript runtime error '800a0009' ...
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...
4
by: Daryl Davis | last post by:
I am having trouble with ReDim (see code below) SaleTable2's structure includes an array for SaleDetailTable2 Dim newsale As New hallsales.SaleTable2 Dim detail As New hallsales.SaleDetailTable2...
5
by: Zenobia | last post by:
Hello, I want to keep a list references to database records being accessed. I will do this by storing the record keys in a list. The list must not contain duplicate keys. So I check the...
9
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
6
by: Adrian | last post by:
Hi In VB 6 I would declare an array in the general part to make it visible to all parts then once I know how elements I had I would redim it with the amount thus Dim testarray() as string ...
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...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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: 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...

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.