redim arrayx?????

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

    redim arrayx?????


    As you might have guessed,
    I am trying to switch from
    vb to c#, but I am having
    a little difficulty.

    How would you resize an array in
    c# ? Is there a "redim preserve"
    as well??

    An example would go a long way.
    Thanks jamie
  • Bruce Wood

    #2
    Re: redim arrayx?????

    You have to do it yourself, manually. You have to create a new array
    and copy the elements into it:

    int[] myArray = new int[5];
    int[] temp = new int[10];
    for (int i = 0; i < myArray.Length; i++)
    {
    temp[i] = myArray[i];
    }
    myArray = temp;

    That's why it's much more common in C# to use ArrayList and convert to
    an array only when you really need an array (for example to pass to
    some method that requires an array).

    Comment

    • Dale Preston

      #3
      Re: redim arrayx?????

      Use an ArrayList. It will resize itself as you need it to. Internally, it
      creates a copy of the array with the new size, and copies the existing data
      to the copy, then renames the copy - which is exactly what a redim preserve
      in VB does internally.

      HTH

      Dale Preston
      MCAD, MCDBA, MCSE

      "jamie" <anonymous@disc ussions.microso ft.com> wrote in message
      news:14f601c540 89$edf74670$a50 1280a@phx.gbl.. .[color=blue]
      >
      > As you might have guessed,
      > I am trying to switch from
      > vb to c#, but I am having
      > a little difficulty.
      >
      > How would you resize an array in
      > c# ? Is there a "redim preserve"
      > as well??
      >
      > An example would go a long way.
      > Thanks jamie[/color]


      Comment

      • jamie

        #4
        Re: redim arrayx?????

        Hum.....
        I think I will have to do a little research on
        the ArrayList.
        Thanks.
        [color=blue]
        >-----Original Message-----
        >Use an ArrayList. It will resize itself as you need it[/color]
        to. Internally, it[color=blue]
        >creates a copy of the array with the new size, and copies[/color]
        the existing data[color=blue]
        >to the copy, then renames the copy - which is exactly[/color]
        what a redim preserve[color=blue]
        >in VB does internally.
        >
        >HTH
        >
        >Dale Preston
        >MCAD, MCDBA, MCSE
        >
        >"jamie" <anonymous@disc ussions.microso ft.com> wrote in[/color]
        message[color=blue]
        >news:14f601c54 089$edf74670$a5 01280a@phx.gbl. ..[color=green]
        >>
        >> As you might have guessed,
        >> I am trying to switch from
        >> vb to c#, but I am having
        >> a little difficulty.
        >>
        >> How would you resize an array in
        >> c# ? Is there a "redim preserve"
        >> as well??
        >>
        >> An example would go a long way.
        >> Thanks jamie[/color]
        >
        >
        >.
        >[/color]

        Comment

        • jamie

          #5
          Re: redim arrayx?????

          I think the recipe is exactly
          what I need to do. I knew I could
          do it that way, I was just hoping
          that I wouldnt have to for clarity
          sake.

          Thanks.
          [color=blue]
          >-----Original Message-----
          >You have to do it yourself, manually. You have to create[/color]
          a new array[color=blue]
          >and copy the elements into it:
          >
          >int[] myArray = new int[5];
          >int[] temp = new int[10];
          >for (int i = 0; i < myArray.Length; i++)
          >{
          > temp[i] = myArray[i];
          >}
          >myArray = temp;
          >
          >That's why it's much more common in C# to use ArrayList[/color]
          and convert to[color=blue]
          >an array only when you really need an array (for example[/color]
          to pass to[color=blue]
          >some method that requires an array).
          >
          >.
          >[/color]

          Comment

          Working...