how to turn ICollection into ArrayList?

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

    how to turn ICollection into ArrayList?

    since ArrayList implements ICollection, is there a quick way to convert an
    ICollection into ArrayList (besides actually iterating through each element
    in the ICollection and explicitly adding them to a new ArrayList instance) ??


  • Cor Ligthert

    #2
    Re: how to turn ICollection into ArrayList?

    Hi,

    Did you ever saw this page?

    addrange


    I hope this helps?

    Cor


    Comment

    • Peter Rilling

      #3
      Re: how to turn ICollection into ArrayList?

      AddRange simply iterates the collection and copies the items.

      MrNobody, I guess the question that I have is, when you refer to "quick",
      are you talking about the fewest number of code lines, or the fewest number
      of program cycles.

      There is no way to automatically turn an ICollection into an ArrayList
      without copying the items, unless the collection was originally an
      ArrayList. But the simplest way might be to use AddRange like Cor mentioned
      below.


      "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
      news:esL3V2c8EH A.2568@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hi,
      >
      > Did you ever saw this page?
      >
      > addrange
      >[/color]
      http://msdn.microsoft.com/library/de...rangetopic.asp[color=blue]
      >
      > I hope this helps?
      >
      > Cor
      >
      >[/color]


      Comment

      • Shariq Khan

        #4
        Re: how to turn ICollection into ArrayList?

        You can use the AddRange method of the ArrayList to add from an object that
        implements an IList interface.

        See


        Shariq Khan
        shariq@shariqkh an.com


        "MrNobody" <MrNobody@discu ssions.microsof t.com> wrote in message
        news:FED3B1C9-5849-41DA-B164-9155999303BA@mi crosoft.com...[color=blue]
        > since ArrayList implements ICollection, is there a quick way to convert an
        > ICollection into ArrayList (besides actually iterating through each
        > element
        > in the ICollection and explicitly adding them to a new ArrayList instance)
        > ??
        >
        >[/color]


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: how to turn ICollection into ArrayList?

          MrNobody,
          In addition to the AddRange method, I would probably simply use the
          ArrayList constructor that accepts an ICollection:

          Dim collection As ICollection
          Dim list As New ArrayList(colle ction)

          For details on this constructor see:



          Hope this helps
          Jay

          "MrNobody" <MrNobody@discu ssions.microsof t.com> wrote in message
          news:FED3B1C9-5849-41DA-B164-9155999303BA@mi crosoft.com...[color=blue]
          > since ArrayList implements ICollection, is there a quick way to convert an
          > ICollection into ArrayList (besides actually iterating through each
          > element
          > in the ICollection and explicitly adding them to a new ArrayList instance)
          > ??
          >
          >[/color]


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: how to turn ICollection into ArrayList?

            Doh!
            I should have given a C# sample :-|

            ICollection collection;
            ArrayList list = new ArrayList(colle ction);

            Jay

            "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            news:uShKBPe8EH A.3076@TK2MSFTN GP15.phx.gbl...[color=blue]
            > MrNobody,
            > In addition to the AddRange method, I would probably simply use the
            > ArrayList constructor that accepts an ICollection:
            >
            > Dim collection As ICollection
            > Dim list As New ArrayList(colle ction)
            >
            > For details on this constructor see:
            >
            > http://msdn.microsoft.com/library/de...ctortopic2.asp
            >
            > Hope this helps
            > Jay
            >
            > "MrNobody" <MrNobody@discu ssions.microsof t.com> wrote in message
            > news:FED3B1C9-5849-41DA-B164-9155999303BA@mi crosoft.com...[color=green]
            >> since ArrayList implements ICollection, is there a quick way to convert
            >> an
            >> ICollection into ArrayList (besides actually iterating through each
            >> element
            >> in the ICollection and explicitly adding them to a new ArrayList
            >> instance) ??
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • MrNobody

              #7
              Re: how to turn ICollection into ArrayList?

              Yeah I was kinda hoping for something that used least cpu cycles- like a type
              cast , butt it's ok because I don't really have THAT many items in this
              collection... Thanks everyone

              Comment

              • Ignacio Machin \( .NET/ C#  MVP \)

                #8
                Re: how to turn ICollection into ArrayList?

                Hi,

                A shallow copy is the only way of doing it, as ArrayList has its own
                internal way to keep the data. Also with a copy the original collection can
                be modified afterwards and the arraylist will not be changed.

                Cheers,

                --
                Ignacio Machin,
                ignacio.machin AT dot.state.fl.us
                Florida Department Of Transportation

                "MrNobody" <MrNobody@discu ssions.microsof t.com> wrote in message
                news:BDDC4EF1-EA9A-4EDF-95C5-1207D845BF41@mi crosoft.com...[color=blue]
                > Yeah I was kinda hoping for something that used least cpu cycles- like a
                > type
                > cast , butt it's ok because I don't really have THAT many items in this
                > collection... Thanks everyone[/color]


                Comment

                Working...