locked
Redim in C#

    Question

  • Hello everyone, is there a way to simulate VBs redim function in C#?

    Thanks

    Thursday, February 01, 2007 3:33 PM

Answers

All replies

  • Check Array.Resize
    Thursday, February 01, 2007 3:41 PM
  • Thank you sir

     

    Thursday, February 01, 2007 4:22 PM
  • Redim is used to resize some array, so while working in .Net or specifically C# i would recomend you using Collections under System.Collecitons.Generics namespace.

    like List<T> class

    Dictionary<T,S> class etc

    example:

    List<int> list = new List<int>(); // create a colleciton to store int....

    list.Add(1); // Add some number

    list.Add(2); // Add another Number.

    This will automatically be resized whenever you add something to it and you don't need Redim anymore here.

    I hope this will help.

    Best Regards,

    Rizwan aka RizwanSharp

    Thursday, February 01, 2007 6:42 PM
  • public static Array Redim(Array origArray, Int32 desiredSize)
            {
                System.Type t = origArray.GetType().GetElementType();
                Array newArray = Array.CreateInstance(t, desiredSize);
                Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, desiredSize));
                return newArray;
            }


    //from Applied Microsoft.NET framework Programming - Jeffrey Richter
    Friday, February 02, 2007 5:38 AM
  • Bear in mind that that's obsolete (only needed for .Net 1.x).

    For .Net 2.x and later, use Array.Resize<T>() as stated previously.
    Friday, February 02, 2007 10:02 AM