Redim Arrays in C#
-
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
-
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
Use an arraylist if you want a resizable array. Christian Graus - Microsoft MVP - C++
-
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
Here's a simple one:
string[] Temp = new string[20];
Array.Copy(MyItems, 0, Temp, 0, MyItems.Length);
MyItems = new string[20] {};
Array.Copy(Temp, 0, MyItems, 0, Temp.Length);
Array.Clear(Temp, 0, Temp.Length);
Temp = null; -
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
This is also something I found, not sure if it'll work but it looks good:
// Reallocates an array with a new size, and copies the contents
// of the old array to the new array.
// Arguments:
// oldArray the old array, to be reallocated.
// newSize the new array size.
// Returns A new array with the same contents.
public static System.Array ResizeArray (System.Array oldArray, int newSize) {
int oldSize = oldArray.Length;
System.Type elementType = oldArray.GetType().GetElementType();
System.Array newArray = System.Array.CreateInstance(elementType,newSize);
int preserveLength = System.Math.Min(oldSize,newSize);
if (preserveLength > 0)
System.Array.Copy (oldArray,newArray,preserveLength);
return newArray;
} -
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
I seconed use
ArrayList
Reiszing array take more time and resource to make copy Operations think of it when you have array with many Items not few ones MCAD -
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
I have written a function for a similar purpose that creates a dynamic array. Here Check this out:
_/// /// Function to add an array element to a dynamic Array /// /// Dynamic Array /// Element to add to the dynamic array /// array with the element appended to it_ public static Array RedimNPreserve(int[] Arr,int Element) { System.Collections.ArrayList al = new System.Collections.ArrayList(); int i; if(Arr!=null) { for(i=0;i<=Arr.GetUpperBound(0);i++) { al.Add(Arr[i]); } } al.Add(Element); return al.ToArray(typeof(int)); }
Elvis (a.k.a. Azerax) Life is Music listen to it before it fades -
Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko
Thank you all for feedback, I have been using arraylists thus far, but the code that i have seen is helpful. Thanks again. Sincerely, Max Pastchenko
-
This is also something I found, not sure if it'll work but it looks good:
// Reallocates an array with a new size, and copies the contents
// of the old array to the new array.
// Arguments:
// oldArray the old array, to be reallocated.
// newSize the new array size.
// Returns A new array with the same contents.
public static System.Array ResizeArray (System.Array oldArray, int newSize) {
int oldSize = oldArray.Length;
System.Type elementType = oldArray.GetType().GetElementType();
System.Array newArray = System.Array.CreateInstance(elementType,newSize);
int preserveLength = System.Math.Min(oldSize,newSize);
if (preserveLength > 0)
System.Array.Copy (oldArray,newArray,preserveLength);
return newArray;
}That method looks very good. Our Instant C# VB to C# converter converts VB redim preserve statements to C# in-line equivalents, via something like the following example: VB: Dim YourArray() As Integer ... ReDim Preserve YourArray(i) C#: int[] YourArray; ... int[] temp = new int[i + 1]; if (YourArray != null) Array.Copy(YourArray, temp, Math.Min(YourArray.Length, temp.Length)); YourArray = temp; (we have to add 1 since VB arrays sizes are specified in terms of their upper bound, not the number of elements) David Anton www.tangiblesoftwaresolutions.com Home of: Clear VB: Cleans up outdated VB.NET code Instant C#: Converts from VB.NET to C# Instant VB: Converts from C# to VB.NET Instant J#: Converts from VB.NET to J#