redimension array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • y

    #1

    redimension array

    Hi,

    I have an array with 2 elements and I would like to keep
    the elements but redimension the array so that I can add
    another value.

    How do I do this.

    I have tried this but, i think I have screwy logic.

    Thanks


    intRedimArray = arr_Selected.Le ngth;
    object tmp_arr = arr_Selected.Cl one();
    arr_Selected (string [])tmp_arr;
    arr_Selected = new string[intRedimArray];

    arr_Selected[intArrayCounter-1] = strToAdd;
    tmp_arr= null;

    the program will keep showing 1.
    MessageBox.Show (arr_Selected.L ength.ToString( ));


    Thanks for your help,

    y

  • Peter Rilling

    #2
    Re: redimension array

    You might be better off using an ArrayList.

    "y" <y@hotmail.co m> wrote in message
    news:097c01c355 23$596910e0$a50 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > I have an array with 2 elements and I would like to keep
    > the elements but redimension the array so that I can add
    > another value.
    >
    > How do I do this.
    >
    > I have tried this but, i think I have screwy logic.
    >
    > Thanks
    >
    >
    > intRedimArray = arr_Selected.Le ngth;
    > object tmp_arr = arr_Selected.Cl one();
    > arr_Selected (string [])tmp_arr;
    > arr_Selected = new string[intRedimArray];
    >
    > arr_Selected[intArrayCounter-1] = strToAdd;
    > tmp_arr= null;
    >
    > the program will keep showing 1.
    > MessageBox.Show (arr_Selected.L ength.ToString( ));
    >
    >
    > Thanks for your help,
    >
    > y
    >[/color]


    Comment

    • Jack Hanebach

      #3
      Re: redimension array

      "y" <y@hotmail.co m> wrote in message
      news:097c01c355 23$596910e0$a50 1280a@phx.gbl.. .
      [color=blue]
      > intRedimArray = arr_Selected.Le ngth;
      > object tmp_arr = arr_Selected.Cl one();
      > arr_Selected (string [])tmp_arr;
      > arr_Selected = new string[intRedimArray];
      >
      > arr_Selected[intArrayCounter-1] = strToAdd;
      > tmp_arr= null;
      >
      > the program will keep showing 1.
      > MessageBox.Show (arr_Selected.L ength.ToString( ));[/color]

      This won't work because the array size for 'new string[intRedimArray]'
      should be a compile time constant. You can use Array.CreateIns tance instead.


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: redimension array

        y,
        To redimension an array you need to create a new array, copy the existing
        elements to the new array, start using the new array.

        Something like:
        string[] list;

        int newLength = list.Length + 5;

        string[] temp = new string[newLength];
        list.CopyTo(tem p, 0);

        list = temp;

        Note, if you are decreasing the size of the array, you can use Array.Copy
        instead of CopyTo.

        Array.Copy(list , 0, temp, 0, newLength);

        Actually Array.Copy works for either increasing or decreasing the array. As
        long as you don't try to copy more elements than what is in the source array
        or the destination array.

        Hope this helps
        Jay

        "y" <y@hotmail.co m> wrote in message
        news:097c01c355 23$596910e0$a50 1280a@phx.gbl.. .[color=blue]
        > Hi,
        >
        > I have an array with 2 elements and I would like to keep
        > the elements but redimension the array so that I can add
        > another value.
        >
        > How do I do this.
        >
        > I have tried this but, i think I have screwy logic.
        >
        > Thanks
        >
        >
        > intRedimArray = arr_Selected.Le ngth;
        > object tmp_arr = arr_Selected.Cl one();
        > arr_Selected (string [])tmp_arr;
        > arr_Selected = new string[intRedimArray];
        >
        > arr_Selected[intArrayCounter-1] = strToAdd;
        > tmp_arr= null;
        >
        > the program will keep showing 1.
        > MessageBox.Show (arr_Selected.L ength.ToString( ));
        >
        >
        > Thanks for your help,
        >
        > y
        >[/color]


        Comment

        • Val Savvateev

          #5
          Re: redimension array

          [color=blue]
          > This won't work because the array size for 'new string[intRedimArray]'
          > should be a compile time constant.[/color]

          I'm affraid this statement is not quite correct....


          Comment

          • Jack Hanebach

            #6
            Re: redimension array

            "Val Savvateev" <1@2.com> wrote in message
            news:utvwW%23TV DHA.624@TK2MSFT NGP10.phx.gbl.. .[color=blue]
            >[color=green]
            > > This won't work because the array size for 'new string[intRedimArray]'
            > > should be a compile time constant.[/color]
            >
            > I'm affraid this statement is not quite correct....[/color]

            I'm affraid you're right... my C++ background is showing :)


            Comment

            Working...