Loading a collection

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

    #1

    Loading a collection

    Ok, here it is. Which is more correct?

    AList = New ArrayList
    for i = 0 to 10
    A = New myObject
    AList.Add(A)
    A = nothing
    next


    --- OR ---

    AList = New ArrayList
    for i = 0 to 10
    A = New myObject
    AList.Add(A)
    next


  • Herfried K. Wagner [MVP]

    #2
    Re: Loading a collection

    "Scott Cupstid" <scupstid@tampa bay.rr.comschri eb:
    Ok, here it is. Which is more correct?
    >
    AList = New ArrayList
    for i = 0 to 10
    A = New myObject
    AList.Add(A)
    A = nothing
    next
    >
    >
    --- OR ---
    >
    AList = New ArrayList
    for i = 0 to 10
    A = New myObject
    AList.Add(A)
    next
    You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
    write:

    \\\
    Dim AList As New ArrayList()
    For i As Integer = 0 To 10
    AList.Add(New MyObject())
    Next i
    ///

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

    Comment

    • Phill W.

      #3
      Re: Loading a collection

      Scott Cupstid wrote:
      AList = New ArrayList
      for i = 0 to 10
      A = New myObject
      AList.Add(A)
      A = nothing
      next
      --- OR ---
      AList = New ArrayList
      for i = 0 to 10
      A = New myObject
      AList.Add(A)
      next
      The latter, since setting object references to Nothing is no longer
      required; Garbage Collection will take care of it.

      If myObject contains unmanaged resources, then a call to ...

      A.Dispose()

      .... might be more appropriate, but if not ...

      AList = New ArrayList
      For i = 0 To 10
      AList.Add(New myObject)
      Next

      .... would do nicely.

      HTH,
      Phill W.

      Comment

      • Lloyd Sheen

        #4
        Re: Loading a collection


        "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
        news:et6jjb$9at $1@south.jnrs.j a.net...
        Scott Cupstid wrote:
        >
        >AList = New ArrayList
        >for i = 0 to 10
        > A = New myObject
        > AList.Add(A)
        > A = nothing
        >next
        >
        >--- OR ---
        >
        >AList = New ArrayList
        >for i = 0 to 10
        > A = New myObject
        > AList.Add(A)
        >next
        >
        The latter, since setting object references to Nothing is no longer
        required; Garbage Collection will take care of it.
        >
        If myObject contains unmanaged resources, then a call to ...
        >
        A.Dispose()
        >
        ... might be more appropriate, but if not ...
        >
        AList = New ArrayList
        For i = 0 To 10
        AList.Add(New myObject)
        Next
        >
        ... would do nicely.
        >
        HTH,
        Phill W.
        I don't think that a dispose is a good idea. What you are doing is creating
        an object, then putting a reference to it in the array. If you dispose it
        then the object could be in a less that useful state.

        Lloyd Sheen

        Comment

        • Scott Cupstid

          #5
          Re: Loading a collection

          Thanks.

          Just trying to get used to VB after doing years of C++ and Delphi.


          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
          news:%23l$OnwYZ HHA.2552@TK2MSF TNGP06.phx.gbl. ..
          "Scott Cupstid" <scupstid@tampa bay.rr.comschri eb:
          >Ok, here it is. Which is more correct?
          >>
          >AList = New ArrayList
          >for i = 0 to 10
          > A = New myObject
          > AList.Add(A)
          > A = nothing
          >next
          >>
          >>
          >--- OR ---
          >>
          >AList = New ArrayList
          >for i = 0 to 10
          > A = New myObject
          > AList.Add(A)
          >next
          >
          You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
          write:
          >
          \\\
          Dim AList As New ArrayList()
          For i As Integer = 0 To 10
          AList.Add(New MyObject())
          Next i
          ///
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Scott Cupstid

            #6
            Re: Loading a collection

            Ok, I think this a little more complicated. I have two lists both
            referencing the same collection of objects. The two lists simply provide
            different traversal paths for the objects. For example, one may be an
            alphabetic listing and the other a numeric listing. How would I load each
            list (givent that each list has it's own method for sorting) without
            actually instantiating multiple instances of the objects?

            Dim List1 as New ArrayList()
            Dim List2 as New ArrayList()
            Dim A as MyObject()
            Dim i as Integer

            for i = 0 to 10
            A = New MyObject()
            List1.Add(A)
            List2.Add(A)
            next

            In this case if I access A from one list, will the changes I make to A be
            recognized by the same instance of A in the second list? Also, if I set a
            reference to A in List1 to nothing, will the reference to the same instance
            of A in the second list be invalid?

            I guess my real question is does VB handle a reference count automatically
            on instances of objects?


            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
            news:%23l$OnwYZ HHA.2552@TK2MSF TNGP06.phx.gbl. ..
            "Scott Cupstid" <scupstid@tampa bay.rr.comschri eb:
            >Ok, here it is. Which is more correct?
            >>
            >AList = New ArrayList
            >for i = 0 to 10
            > A = New myObject
            > AList.Add(A)
            > A = nothing
            >next
            >>
            >>
            >--- OR ---
            >>
            >AList = New ArrayList
            >for i = 0 to 10
            > A = New myObject
            > AList.Add(A)
            >next
            >
            You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
            write:
            >
            \\\
            Dim AList As New ArrayList()
            For i As Integer = 0 To 10
            AList.Add(New MyObject())
            Next i
            ///
            >
            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • Lloyd Sheen

              #7
              Re: Loading a collection


              "Scott Cupstid" <scupstid@tampa bay.rr.comwrote in message
              news:eHPyzZZZHH A.3272@TK2MSFTN GP03.phx.gbl...
              Ok, I think this a little more complicated. I have two lists both
              referencing the same collection of objects. The two lists simply provide
              different traversal paths for the objects. For example, one may be an
              alphabetic listing and the other a numeric listing. How would I load each
              list (givent that each list has it's own method for sorting) without
              actually instantiating multiple instances of the objects?
              >
              Dim List1 as New ArrayList()
              Dim List2 as New ArrayList()
              Dim A as MyObject()
              Dim i as Integer
              >
              for i = 0 to 10
              A = New MyObject()
              List1.Add(A)
              List2.Add(A)
              next
              >
              In this case if I access A from one list, will the changes I make to A be
              recognized by the same instance of A in the second list? Also, if I set a
              reference to A in List1 to nothing, will the reference to the same
              instance of A in the second list be invalid?
              >
              I guess my real question is does VB handle a reference count automatically
              on instances of objects?
              >
              >
              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
              news:%23l$OnwYZ HHA.2552@TK2MSF TNGP06.phx.gbl. ..
              >"Scott Cupstid" <scupstid@tampa bay.rr.comschri eb:
              >>Ok, here it is. Which is more correct?
              >>>
              >>AList = New ArrayList
              >>for i = 0 to 10
              >> A = New myObject
              >> AList.Add(A)
              >> A = nothing
              >>next
              >>>
              >>>
              >>--- OR ---
              >>>
              >>AList = New ArrayList
              >>for i = 0 to 10
              >> A = New myObject
              >> AList.Add(A)
              >>next
              >>
              >You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
              >write:
              >>
              >\\\
              >Dim AList As New ArrayList()
              >For i As Integer = 0 To 10
              > AList.Add(New MyObject())
              >Next i
              >///
              >>
              >--
              >M S Herfried K. Wagner
              >M V P <URL:http://dotnet.mvps.org/>
              >V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
              >
              >
              Scott, think of it this way. You create the object once. At that point it
              most likely is "resident" in a variable. Now you can add it to as many
              collections as you wish. You could later on find an object in a collection
              and then add it to another. Since each collection has its own sorting each
              list can show the objects in a specific order.

              The other good thing -- update the object once. Now if you update info you
              should know where it is shown since changing a field may change the display
              characteristics of that object.

              Lloyd Sheen

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Loading a collection

                Yes


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: Loading a collection

                  Exactly as I would do it,

                  Cor

                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
                  news:%23l$OnwYZ HHA.2552@TK2MSF TNGP06.phx.gbl. ..
                  "Scott Cupstid" <scupstid@tampa bay.rr.comschri eb:
                  >Ok, here it is. Which is more correct?
                  >>
                  >AList = New ArrayList
                  >for i = 0 to 10
                  > A = New myObject
                  > AList.Add(A)
                  > A = nothing
                  >next
                  >>
                  >>
                  >--- OR ---
                  >>
                  >AList = New ArrayList
                  >for i = 0 to 10
                  > A = New myObject
                  > AList.Add(A)
                  >next
                  >
                  You do not need to set 'A' to 'Nothing' if it is a local variable. I'd
                  write:
                  >
                  \\\
                  Dim AList As New ArrayList()
                  For i As Integer = 0 To 10
                  AList.Add(New MyObject())
                  Next i
                  ///
                  >
                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: Loading a collection

                    "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kschrieb:
                    >AList = New ArrayList
                    >for i = 0 to 10
                    > A = New myObject
                    > AList.Add(A)
                    > A = nothing
                    >next
                    >
                    >--- OR ---
                    >
                    >AList = New ArrayList
                    >for i = 0 to 10
                    > A = New myObject
                    > AList.Add(A)
                    >next
                    >
                    The latter, since setting object references to Nothing is no longer
                    required; Garbage Collection will take care of it.

                    It was not even necessary in VB6 if 'A' was a local variable.

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

                    Comment

                    Working...