Redim Preserve Array

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

    Redim Preserve Array

    Hello,

    What is C# equivalent of rediming an array and preserving exciting elements.
    VB.NET syntax looks something like this:
    ReDim Preserve myArray(5)
    Thank you


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Redim Preserve Array

    LP,

    You will want to use an ArrayList, which can be found in the
    System.Collecti ons namespace. When it returns a value, however, you will
    have to cast that value to the type that is stored in it.

    In .NET 2.0, you would use a List<T> instance (where T is the type you
    want to store in the array), which can be found in the
    System.Collecti ons.Generic namespace.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "LP" <lp@a.com> wrote in message
    news:eSn9y8FEFH A.1264@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hello,
    >
    > What is C# equivalent of rediming an array and preserving exciting
    > elements.
    > VB.NET syntax looks something like this:
    > ReDim Preserve myArray(5)
    > Thank you
    >
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Redim Preserve Array

      "LP" <lp@a.com> schrieb:[color=blue]
      > What is C# equivalent of rediming an array and preserving exciting
      > elements.
      > VB.NET syntax looks something like this:
      > ReDim Preserve myArray(5)[/color]

      First, create a new array of the desired size. Then use 'Array.Copy' to
      copy the contents of the "old" array to the new array. After doing that,
      assign the new array to the variable that is currently referencing the old
      array.

      If the number of items in an array changes frequently, consider using a more
      dynamic datastructure, like an arraylist or one of the other collections
      available in the 'System.Collect ions' namespace.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Urs Vogel

        #4
        Re: Redim Preserve Array

        LP

        There's nothing like it in C#, you have to build it own your own, if you
        want to use plain arrays. But why don't you use the ArrayList class?

        Urs

        "LP" <lp@a.com> schrieb im Newsbeitrag
        news:eSn9y8FEFH A.1264@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hello,
        >
        > What is C# equivalent of rediming an array and preserving exciting
        > elements.
        > VB.NET syntax looks something like this:
        > ReDim Preserve myArray(5)
        > Thank you
        >
        >[/color]


        Comment

        • LP

          #5
          Re: Redim Preserve Array

          Thank you all,

          I would like to use ArrayList, but I am getting an array of strings from a
          VB.NET class that someone else developed. I need to do further manipulation
          on this array, add more elements and then pass it to another object as Array
          of strings again that will store its data to SQL db.
          I don't know if it makes sense to convert this Array to ArrayList and then
          back to another Array.
          The best option at this point (besides rewriting 2 objects) is to use
          Array.Copy. Any other ideas?

          "Urs Vogel" <uvogel@msn.com > wrote in message
          news:OT6oGPGEFH A.512@TK2MSFTNG P15.phx.gbl...[color=blue]
          > LP
          >
          > There's nothing like it in C#, you have to build it own your own, if you
          > want to use plain arrays. But why don't you use the ArrayList class?
          >
          > Urs
          >
          > "LP" <lp@a.com> schrieb im Newsbeitrag
          > news:eSn9y8FEFH A.1264@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Hello,
          > >
          > > What is C# equivalent of rediming an array and preserving exciting
          > > elements.
          > > VB.NET syntax looks something like this:
          > > ReDim Preserve myArray(5)
          > > Thank you
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Redim Preserve Array

            LP,

            The best choice is to use the ArrayList class, do the work you need to
            do, and then get the array back by using the ToArray method on the ArrayList
            class.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "LP" <lp@a.com> wrote in message
            news:erUXeeGEFH A.1188@tk2msftn gp13.phx.gbl...[color=blue]
            > Thank you all,
            >
            > I would like to use ArrayList, but I am getting an array of strings from a
            > VB.NET class that someone else developed. I need to do further
            > manipulation
            > on this array, add more elements and then pass it to another object as
            > Array
            > of strings again that will store its data to SQL db.
            > I don't know if it makes sense to convert this Array to ArrayList and then
            > back to another Array.
            > The best option at this point (besides rewriting 2 objects) is to use
            > Array.Copy. Any other ideas?
            >
            > "Urs Vogel" <uvogel@msn.com > wrote in message
            > news:OT6oGPGEFH A.512@TK2MSFTNG P15.phx.gbl...[color=green]
            >> LP
            >>
            >> There's nothing like it in C#, you have to build it own your own, if you
            >> want to use plain arrays. But why don't you use the ArrayList class?
            >>
            >> Urs
            >>
            >> "LP" <lp@a.com> schrieb im Newsbeitrag
            >> news:eSn9y8FEFH A.1264@TK2MSFTN GP12.phx.gbl...[color=darkred]
            >> > Hello,
            >> >
            >> > What is C# equivalent of rediming an array and preserving exciting
            >> > elements.
            >> > VB.NET syntax looks something like this:
            >> > ReDim Preserve myArray(5)
            >> > Thank you
            >> >
            >> >[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • LP

              #7
              Re: Redim Preserve Array

              Yes, that would the best choice, thank you!

              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
              message news:eZh2mrGEFH A.3732@TK2MSFTN GP14.phx.gbl...[color=blue]
              > LP,
              >
              > The best choice is to use the ArrayList class, do the work you need to
              > do, and then get the array back by using the ToArray method on the[/color]
              ArrayList[color=blue]
              > class.
              >
              >
              > --
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >
              > "LP" <lp@a.com> wrote in message
              > news:erUXeeGEFH A.1188@tk2msftn gp13.phx.gbl...[color=green]
              > > Thank you all,
              > >
              > > I would like to use ArrayList, but I am getting an array of strings from[/color][/color]
              a[color=blue][color=green]
              > > VB.NET class that someone else developed. I need to do further
              > > manipulation
              > > on this array, add more elements and then pass it to another object as
              > > Array
              > > of strings again that will store its data to SQL db.
              > > I don't know if it makes sense to convert this Array to ArrayList and[/color][/color]
              then[color=blue][color=green]
              > > back to another Array.
              > > The best option at this point (besides rewriting 2 objects) is to use
              > > Array.Copy. Any other ideas?
              > >
              > > "Urs Vogel" <uvogel@msn.com > wrote in message
              > > news:OT6oGPGEFH A.512@TK2MSFTNG P15.phx.gbl...[color=darkred]
              > >> LP
              > >>
              > >> There's nothing like it in C#, you have to build it own your own, if[/color][/color][/color]
              you[color=blue][color=green][color=darkred]
              > >> want to use plain arrays. But why don't you use the ArrayList class?
              > >>
              > >> Urs
              > >>
              > >> "LP" <lp@a.com> schrieb im Newsbeitrag
              > >> news:eSn9y8FEFH A.1264@TK2MSFTN GP12.phx.gbl...
              > >> > Hello,
              > >> >
              > >> > What is C# equivalent of rediming an array and preserving exciting
              > >> > elements.
              > >> > VB.NET syntax looks something like this:
              > >> > ReDim Preserve myArray(5)
              > >> > Thank you
              > >> >
              > >> >
              > >>
              > >>[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...