Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Redim Arrays in C#

Redim Arrays in C#

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpastchenko
    wrote on last edited by
    #1

    Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

    T C M A M 6 Replies Last reply
    0
    • M mpastchenko

      Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Use an arraylist if you want a resizable array. Christian Graus - Microsoft MVP - C++

      1 Reply Last reply
      0
      • M mpastchenko

        Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

        T Offline
        T Offline
        Tim McCurdy
        wrote on last edited by
        #3

        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;

        1 Reply Last reply
        0
        • M mpastchenko

          Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

          T Offline
          T Offline
          Tim McCurdy
          wrote on last edited by
          #4

          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;
          }

          D 1 Reply Last reply
          0
          • M mpastchenko

            Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

            M Offline
            M Offline
            Mohamad Al Husseiny
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • M mpastchenko

              Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

              A Offline
              A Offline
              Azerax
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M mpastchenko

                Greetings, I am curious if someone has a funtion implemented in C# similar Array REDIM in VB? Sincerely, Max Pastchenko

                M Offline
                M Offline
                mpastchenko
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • T Tim McCurdy

                  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;
                  }

                  D Offline
                  D Offline
                  Dave Doknjas
                  wrote on last edited by
                  #8

                  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#

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups