Solved

# ReDim Preserve(vb6)  in  C#

Posted on 2004-09-13
1,569 Views
Hi

how can i do like "vb6"   (ReDim PreserveArray)   but  in  C#
not with ArrayList

regards
0
Question by:Kobi55
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 5

Expert Comment

ID: 12043447
If you just want to change the array length please test code below

int [] a=int[16];
a=int[32];
a[31]=12;

you can create a new int array and assigned to a.
you can change int to other types

0

LVL 3

Expert Comment

ID: 12044428
VB's ReDim Preserve compiles to the following

int[] a = int[16];

// This is the redim stuff
int[] b = int[16];
for (int I = 0; I < a.Length && I < b.Length; I++)
b[I] = a[I];
0

LVL 3

Expert Comment

ID: 12044434

int[] a = int[16];

// This is the redim stuff
int[] b = int[16];
for (int I = 0; I < a.Length && I < b.Length; I++)
b[I] = a[I];

a = b;
0

LVL 3

Assisted Solution

a_goat earned 125 total points
ID: 12044450
Bah, I'm a lazy typist

int[] a = int[16];
// Populate a

// This is the redim stuff
int[] b = int[32];
for (int I = 0; I < a.Length && I < b.Length; I++)
b[I] = a[I];

a = b;

At this point, a will be an int array with 32 slots, containing whatever data was in a when it only had 16 slots.  This will also cover cases where you decrease the number of slots.
0

LVL 5

Assisted Solution

tzxie2000 earned 125 total points
ID: 12044555
if you want remain data in array
use this improve my code may be more quick

int [] a=int[16];
//doing something
a=int[32];

Array.Copy(a,b,a.Length>b.Length?b.Length:a.Length);
a[31]=12;
0

LVL 14

Accepted Solution

AvonWyss earned 250 total points
ID: 12044568
Use the following method:

public static Array ReDimPreserve(Array input, int size) {
Array result=(Array)Activator.CreateInstance(input.GetType(), new object[] {size});
Array.Copy(input, result, Math.Min(input.Length, result.Length));
return result;
}

And then you use it like this:

int [] a=int[16];
a=(int[])ReDimArrayPreserve(a, 32);

Note that this works for arrays of any type, not only ints. because of that, you need to typecast the returned array into the array type you're expecting.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
###### Suggested Courses
Course of the Month7 days, 16 hours left to enroll