473,385 Members | 2,044 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,385 software developers and data experts.

Resizing arrays in C#

H

I'm a former VB programmer and I have a issue with Arrays in C

If I can't tell the size in advance how can I solve the issue since C# does not suppor
resizing arrays like VB do (i.e. Redim Preserve MyArray(x)

Kjell
Nov 16 '05 #1
7 12138
Use an ArrayList.

using System.Collections;

ArrayList al = new ArrayList();
al.Add("one");
al.Add("two");
al.RemoveAt(1); // remove "two"
al.Add("three");
foreach(string s in al)
{
Console.WriteLine(s);
}

If you then need a regular array, use the ToArray() method.

--
William Stacey, MVP

"Kjell" <an*******@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
Hi

I'm a former VB programmer and I have a issue with Arrays in C#

If I can't tell the size in advance how can I solve the issue since C# does not support resizing arrays like VB do (i.e. Redim Preserve MyArray(x))

Kjell


Nov 16 '05 #2
You can use an arraylist or if you want a typed array you can use the copyto
method of the array.
array1 (10 long)
array 2 (11 long)
copy array1 into array two and you have a resized array
HTH
JB
"Kjell" <an*******@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
Hi

I'm a former VB programmer and I have a issue with Arrays in C#

If I can't tell the size in advance how can I solve the issue since C# does not support resizing arrays like VB do (i.e. Redim Preserve MyArray(x))

Kjell

Nov 16 '05 #3
> Kjellwrote:
Hi

I'm a former VB programmer and I have a issue with Arrays in C#

If I can't tell the size in advance how can I solve the issue since C# does not support resizing arrays like VB do (i.e. Redim Preserve MyArray(x))

Kjell


As John said, an ArrayList may be more useful to you, but if you want
to change array sizes on the fly then here's an example from Instant
C#, the vb.net to C# converter:

VB Code:
Dim x() As Integer
ReDim Preserve x(3)

C# Code:
//INSTANT C# NOTE: The following 4 lines reproduce what 'ReDim
Preserve' does behind the scenes in VB.NET:
//ORIGINAL LINE: ReDim Preserve x(3)
int[] x = null;
int[] Temp1 = new int[4];
if (x != null)
System.Array.Copy(x, Temp1, x.Length);
x = Temp1;

Nov 16 '05 #4
Cat

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:en*************@TK2MSFTNGP12.phx.gbl...
Use an ArrayList.

using System.Collections;

ArrayList al = new ArrayList();
al.Add("one");
al.Add("two");
al.RemoveAt(1); // remove "two"
al.Add("three");
foreach(string s in al)
{
Console.WriteLine(s);
}

If you then need a regular array, use the ToArray() method.
Sorry to interrupt but was just wondering.. how do you specify the type of
array you want returned? Say, you want a string[] returned..
--
William Stacey, MVP

"Kjell" <an*******@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
Hi

I'm a former VB programmer and I have a issue with Arrays in C#

If I can't tell the size in advance how can I solve the issue since C#

does not support
resizing arrays like VB do (i.e. Redim Preserve MyArray(x))

Kjell

Nov 16 '05 #5
Cat <lo***********@yahoo.co.uk> wrote:
If you then need a regular array, use the ToArray() method.
Sorry to interrupt but was just wondering.. how do you specify the type of
array you want returned? Say, you want a string[] returned..


Use myArrayList.ToArray(typeof(string))

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
The same way VB does it: create a new array with different size, and copy
all the data (that is exactly what Redim does).
You can also have this transparently done hidden away from you using an
ArrayList.

Niki

"Kjell" <an*******@discussions.microsoft.com> wrote in
news:F7**********************************@microsof t.com...
Hi

I'm a former VB programmer and I have a issue with Arrays in C#

If I can't tell the size in advance how can I solve the issue since C# does not support resizing arrays like VB do (i.e. Redim Preserve MyArray(x))

Kjell

Nov 16 '05 #7
Cat

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Cat <lo***********@yahoo.co.uk> wrote:
If you then need a regular array, use the ToArray() method.
Sorry to interrupt but was just wondering.. how do you specify the type
of array you want returned? Say, you want a string[] returned..


Use myArrayList.ToArray(typeof(string))

--


Cheers :)
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

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

Similar topics

10
by: riki | last post by:
Hi, i have a big problem...i'm using one jscript for resizing of all of my pics in popUp...in main html i'm having many little pics and clicking on them they open in popUp and resize to larger...
0
by: TJ Talluto | last post by:
<facts> I have a "month calendar" that always displays exactly 42 days... and alongside is a vertical box that displays the detail (form fields) of any particular select event that appears on the...
46
by: Blue Ocean | last post by:
How do I do it? I'm doing a practice problem which includes me implementing a set of ints as a class. I don't want to use vector because that would be cheating. ;) I don't want to spend...
5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
11
by: Sharon | last post by:
I'm writing a new control derived from UserControl. I need to get an event when the control is done resizing. I tried the Resize, SizeChanged, Move and the Layout events and I also tried to...
4
by: John A Grandy | last post by:
As there is no ReDim in C# , what is the preferred technique for dynmically increasing the size of any array ? Does the answer depend on whether the array holds primitives versus instances of...
9
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from...
0
by: mdwhite76 | last post by:
Hello, I have a 2 dimensional array that I need to add more elements to. Array.Copy and Array.Resize only work for one dimensional arrays. Any thoughts as to how I can accomplish this with a 2...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
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
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
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...

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.