Results 1 to 10 of 10

Thread: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Resolved [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    Is there a way to resize an array with losing the data in the array.
    I need to resize as the data comes and making the array bigger then it will ever hold will not work.

    Any ideas?

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Changing The Size Of An Array Without Losing The Data?

    you should use an ArrayList.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Changing The Size Of An Array Without Losing The Data?

    Code:
         public static Array ReDimAndPreserve(Array inputArray, int newsize) {
              Array result=(Array)Activator.CreateInstance(inputArray.GetType(), new object[] {newsize});
              Array.Copy(inputArray, result, Math.Min(inputArray.Length, result.Length));
              return result;
         }
    I called it RedimAndPreserve because of an equivalent function in VB6/VB.NET which does this.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Changing The Size Of An Array Without Losing The Data?

    You probably should use a collection for this, but note that if you do use mendhak's method then don't just increase the size of the array by one each time. You should do something like what collections do with their Capacity property. With an ArrayList the Capacity defaults to 16 unless you specify otherwise. When you try to add an item that goes beyond that capacity the collection allocates enough memory to double the capacity. You should do something similar with your array if you use one. If not doubling it each time at least increasing it by a block rather than a single element at a time. You should also note that the ArrayList has a ToArray method, so you can use an ArrayList and still get an array once you're finished if you specifically need an array for some reason.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    Thanks guys. I appreciate the help

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    #1 rule in optimising:
    - Don't use collections when you can get away with arrays.
    I don't live here any more.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    They are alot slower?

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    With ease and maintenance (collections), you make certain sacrifices along the way.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    In your average GUI app you would not notice the difference between using an array and a collection. Even using ReDim Preserve on an array repeatedly probably wouldn't make a noticable difference unless the array was very big. That certainly doesn't mean that should not try to write efficient code. It is a fact that using a collection is easier because you don't have to think about the size issue. If you know the size you need or can make a pretty good guess then using an array definitely would be preferable, particularly for value types, which would need to be boxed in a collection. I'm not sure how many times you would have to resize an array before a collection would become more efficient.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?

    I just realised that I was using VB.NET terminology in the C# forum. A point of interst for users of .NET 2.0 is that the Array class now has a Resize method that works just like ReDim Preserve in VB.NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width