Solved

ReDim Preserve(vb6)  in  C#

Posted on 2004-09-13
6
1,563 Views
Last Modified: 2008-01-09
Hi

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

regards
0
Comment
Question by:Kobi55
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:tzxie2000
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

by:a_goat
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

by:a_goat
ID: 12044434
Sorry, to add to that...


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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:a_goat
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

by:tzxie2000
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

by:
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Error 11 47
Connection String 16 43
how can i get the executable application's path in a specific form? ('\\' instead of '\ ') 5 29
Exit the loop 4 34
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now