resizing arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: resizing arrays

  1. #1
    Join Date
    Jan 2013
    Posts
    7

    resizing arrays

    im looking for a way to resize an array e.g. from myarray[10,10] to myarray[20,20] while preserving the contents , by all accounts the best way is to use the array.copy. i found a snippet of code on a answer elsewhere which says :-


    Code:
    public static void ResizeArray<T>(ref T[,] original, int cols, int rows)
            {
                T[,] newArray = new T[rows, cols];
                //copy the contents of the old array to the new one
                Array.Copy(original, newArray, original.Length);
                //set the original to the new array
                original = newArray;
            }
    how do i pass my array into it ?

    Resize_array<int>( [,] myarray , new rows, newcols)
    ??

    i can see how the swap works but what happens to the old array, does garbage collection dispose of it or should i be handling that.

    Does anyone have a better more efficient way ?

    tia
    Last edited by jonnydexter; February 6th, 2013 at 05:07 PM.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: resizing arrays

    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jan 2013
    Posts
    7

    Re: resizing arrays

    thanks, i had already looked at that post which did enlighten me a little but it does not show how to call the routine the are discussing.

    i have tried various ways at passing my array in but i cant get the syntax correct.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,480

    Re: resizing arrays

    Quote Originally Posted by jonnydexter View Post
    thanks, i had already looked at that post which did enlighten me a little but it does not show how to call the routine the are discussing.

    i have tried various ways at passing my array in but i cant get the syntax correct.
    Did you use the 'ref' keyword?

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center