object = nothing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    object = nothing?

    Hi all,

    Coming from the good old VB6 days we were told to always destroy our objects
    as follows:-

    Dim obj as New MyObject
    ' do some work with obj
    obj = Nothing

    I have been doing this in .Net also for quite a while now. The other day one
    of my peers mentioned that this is a BIG NO NO and that I should let the
    Garbage Collection process handle this for me.

    Can someone share any insight on this?

    TIA!


  • Tim Patrick

    #2
    Re: object = nothing?

    It is safe to set your objects to Nothing. They will immediately be marked
    for garbage collection. If you skip the assignment to Nothing, they will
    still be marked for gargabe collection once they go out of scope and there
    are no other references to the objects. For most local variables, this occurs
    when you exit the function or subroutine.

    Some objects need a little more attention. If an object supports the IDisposable
    interface, it includes a Dispose method that should be called before you
    are finished with it. This guarantees that any acquired resources are released
    before garbage collection. The new Visual Basic 2005 Using keyword will help
    with this process.

    -----
    Tim Patrick
    Start-to-Finish Visual Basic 2005
    Hi all,
    >
    Coming from the good old VB6 days we were told to always destroy our
    objects as follows:-
    >
    Dim obj as New MyObject
    ' do some work with obj
    obj = Nothing
    I have been doing this in .Net also for quite a while now. The other
    day one of my peers mentioned that this is a BIG NO NO and that I
    should let the Garbage Collection process handle this for me.
    >
    Can someone share any insight on this?
    >
    TIA!
    >

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: object = nothing?

      <param@communit y.nospamschrieb :
      Coming from the good old VB6 days we were told to always destroy our
      objects as follows:-
      >
      Dim obj as New MyObject
      ' do some work with obj
      obj = Nothing
      >
      I have been doing this in .Net also for quite a while now. The other day
      one of my peers mentioned that this is a BIG NO NO and that I should let
      the Garbage Collection process handle this for me.
      Your peer is right and he isn't.

      Note that COM used reference counting to detect whether or not an object
      should be destroyed. .NET uses a different approach. The garbage collector
      checks objects in certain periods of time and destroys them if necessary
      (this is simplified, I suggest to read the chapters about garbage collection
      and the garbage collector (GC) in the documentation). Thus setting a
      variable to 'Nothing' does not necessarily destroy the object immediately if
      no other references are pointing to it.

      Because of the evolvedness of .NET's garbage collector it is not even
      necessary to clear all references to an object to be able to destroy it. If
      two objects reference each other but are not reachable from the
      application's code any more, the garbage collector will detect this and will
      clean up the objects after some time.

      So the conclusion is: Setting local variables to 'Nothing' at the end of
      the procedure does not make any sense. This applies to VB6 too. Setting
      variables to 'Nothing' prior to assigning another reference to the variable
      does not make any sense at all too, even not in VB6. It may however make
      sense to set private variables or properties to 'Nothing' to speed up
      cleanup and maintain a valid state.

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

      Comment

      • Göran Andersson

        #4
        Re: object = nothing?

        Herfried K. Wagner [MVP] wrote:
        Setting variables to 'Nothing' prior to assigning another reference to
        the variable does not make any sense at all too, even not in VB6.
        Actually, there is a situation in VB6 where it does make sense. Example:

        Set objRs = objConnection.E xecute("select something from sometable")
        lngValue = objRs(0)
        objRs.Close()
        Set objRs = Nothing
        Set objRs = objConnection.E xecute("select something from someothertable" )
        lngValue2 = objRs(0)
        objRs.Close()

        Removing the reference before creating a new recordset will return the
        first recordset to the object pool. When the next recordset is created,
        it can be reused from the object pool. If you don't remove the
        reference, the first recordset will not be returned to the object pool
        until after the second has been created.

        Comment

        • Guest's Avatar

          #5
          Re: object = nothing?

          So in theory setting an object to Nothing, if not anything should help the
          GC in doing its job right? Since objects will be better identified as
          candidates for garbage collection?



          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
          news:uz9ZBqV9GH A.4712@TK2MSFTN GP03.phx.gbl...
          <param@communit y.nospamschrieb :
          >Coming from the good old VB6 days we were told to always destroy our
          >objects as follows:-
          >>
          >Dim obj as New MyObject
          >' do some work with obj
          >obj = Nothing
          >>
          >I have been doing this in .Net also for quite a while now. The other day
          >one of my peers mentioned that this is a BIG NO NO and that I should let
          >the Garbage Collection process handle this for me.
          >
          Your peer is right and he isn't.
          >
          Note that COM used reference counting to detect whether or not an object
          should be destroyed. .NET uses a different approach. The garbage
          collector checks objects in certain periods of time and destroys them if
          necessary (this is simplified, I suggest to read the chapters about
          garbage collection and the garbage collector (GC) in the documentation).
          Thus setting a variable to 'Nothing' does not necessarily destroy the
          object immediately if no other references are pointing to it.
          >
          Because of the evolvedness of .NET's garbage collector it is not even
          necessary to clear all references to an object to be able to destroy it.
          If two objects reference each other but are not reachable from the
          application's code any more, the garbage collector will detect this and
          will clean up the objects after some time.
          >
          So the conclusion is: Setting local variables to 'Nothing' at the end of
          the procedure does not make any sense. This applies to VB6 too. Setting
          variables to 'Nothing' prior to assigning another reference to the
          variable does not make any sense at all too, even not in VB6. It may
          however make sense to set private variables or properties to 'Nothing' to
          speed up cleanup and maintain a valid state.
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Michel Posseth  [MCP]

            #6
            Re: object = nothing?

            Hello ,,,

            Well you have started something :-) ....... this has been discussed manny
            times in this group and it always ends the same :-)

            The below is my opinion , wich is formed by following a lot of these
            discussions and how it is described in the manny books i own

            The BIG NO NO is a bit odd , he would be right if he would have said that
            it isn`t necesary and that you are typing a line to much
            if your object is declared in method scope

            but there are situations thinkable where it is valid to set a object to
            Nothing ( however never absolut necesary , as it could have been in VB6 )

            so here are some scenario`s for you to rethink :

            Example A
            Private sub Useobject ()
            dim foo as new Objfoo.lib

            ---- use the foo object

            --- setting the foo object here to nothing is absolute not necesary
            --- as soon as it runs out of scope it is marked for collection
            end sub

            Example B

            Private sub Useobject ()
            dim foo as new Objfoo.lib

            for i as integer = 0 to 1000

            if i < 500 then
            ---- use the foo object
            else
            ---- stop using the foo object
            end if
            next
            end sub

            --- here is a situation where it is valid to set the foo object to nothing
            in the else statement ( if i = 500 then foo=Nothing )
            --- especially when it is a resource hungry object , However it is not
            guaranteed that the object is collected at this point
            end sub

            Example C
            The object is declared in class scope ( because you use it withevents or
            whatever reasson you might have )
            and for some reasson you can live without it , for a time and if needed you
            can declare a new instance
            i encountered this scenario myself in a remoting scenario wich used a
            singleton


            if you use VB.Net 2005 then learn yourself to use the using stetement ( it
            makes sure everything is nicely cleaned up , and as a bonus it makes your
            code nicer readable )

            example

            Private sub Useobject ()
            using foo as new Objfoo.lib

            ---- use the foo object

            end using
            end sub


            As said the above is my own opinion without warranty`s especially about
            example B i get a lot of nasty responses however it is a Balena example
            written in the core reference guide of VB.net,, so i guess it should be a
            valid situation .

            I know this thread is going to explode ( as it normally does on this
            subject ) with all people telling you something different , i would say form
            your own opinion
            by reading about the GC on MSDN then you will see what it does and what it
            doesn`t , and especially this is verry important , setting a object to
            Nothing does not mean that you will get your resources inmediatly back ,
            however in resource hungry systems it might make a difference in the right
            scenario .

            regards

            Michel Posseth [MCP]






            <param@communit y.nospamschreef in bericht
            news:eGGwBDV9GH A.1172@TK2MSFTN GP03.phx.gbl...
            Hi all,
            >
            Coming from the good old VB6 days we were told to always destroy our
            objects as follows:-
            >
            Dim obj as New MyObject
            ' do some work with obj
            obj = Nothing
            >
            I have been doing this in .Net also for quite a while now. The other day
            one of my peers mentioned that this is a BIG NO NO and that I should let
            the Garbage Collection process handle this for me.
            >
            Can someone share any insight on this?
            >
            TIA!
            >

            Comment

            • Guest's Avatar

              #7
              Re: object = nothing?

              So forgive my ignorance here, but I am presuming VB 2005 comes with Visual
              Studio 2005 and .net 2.0 framework right? What does the Using statement do?
              Can I use it for both Public and Private variable declarations?


              "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
              news:OsjqzQc9GH A.4572@TK2MSFTN GP02.phx.gbl...
              Hello ,,,
              >
              Well you have started something :-) ....... this has been discussed
              manny times in this group and it always ends the same :-)
              >
              The below is my opinion , wich is formed by following a lot of these
              discussions and how it is described in the manny books i own
              >
              The BIG NO NO is a bit odd , he would be right if he would have said
              that it isn`t necesary and that you are typing a line to much
              if your object is declared in method scope
              >
              but there are situations thinkable where it is valid to set a object to
              Nothing ( however never absolut necesary , as it could have been in
              VB6 )
              >
              so here are some scenario`s for you to rethink :
              >
              Example A
              Private sub Useobject ()
              dim foo as new Objfoo.lib
              >
              ---- use the foo object
              >
              --- setting the foo object here to nothing is absolute not necesary
              --- as soon as it runs out of scope it is marked for collection
              end sub
              >
              Example B
              >
              Private sub Useobject ()
              dim foo as new Objfoo.lib
              >
              for i as integer = 0 to 1000
              >
              if i < 500 then
              ---- use the foo object
              else
              ---- stop using the foo object
              end if
              next
              end sub
              >
              --- here is a situation where it is valid to set the foo object to nothing
              in the else statement ( if i = 500 then foo=Nothing )
              --- especially when it is a resource hungry object , However it is not
              guaranteed that the object is collected at this point
              end sub
              >
              Example C
              The object is declared in class scope ( because you use it withevents or
              whatever reasson you might have )
              and for some reasson you can live without it , for a time and if needed
              you can declare a new instance
              i encountered this scenario myself in a remoting scenario wich used a
              singleton
              >
              >
              if you use VB.Net 2005 then learn yourself to use the using stetement
              ( it makes sure everything is nicely cleaned up , and as a bonus it makes
              your code nicer readable )
              >
              example
              >
              Private sub Useobject ()
              using foo as new Objfoo.lib
              >
              ---- use the foo object
              >
              end using
              end sub
              >
              >
              As said the above is my own opinion without warranty`s especially about
              example B i get a lot of nasty responses however it is a Balena example
              written in the core reference guide of VB.net,, so i guess it should be a
              valid situation .
              >
              I know this thread is going to explode ( as it normally does on this
              subject ) with all people telling you something different , i would say
              form your own opinion
              by reading about the GC on MSDN then you will see what it does and what
              it doesn`t , and especially this is verry important , setting a object to
              Nothing does not mean that you will get your resources inmediatly back ,
              however in resource hungry systems it might make a difference in the right
              scenario .
              >
              regards
              >
              Michel Posseth [MCP]
              >
              >
              >
              >
              >
              >
              <param@communit y.nospamschreef in bericht
              news:eGGwBDV9GH A.1172@TK2MSFTN GP03.phx.gbl...
              >Hi all,
              >>
              >Coming from the good old VB6 days we were told to always destroy our
              >objects as follows:-
              >>
              >Dim obj as New MyObject
              >' do some work with obj
              >obj = Nothing
              >>
              >I have been doing this in .Net also for quite a while now. The other day
              >one of my peers mentioned that this is a BIG NO NO and that I should let
              >the Garbage Collection process handle this for me.
              >>
              >Can someone share any insight on this?
              >>
              >TIA!
              >>
              >
              >

              Comment

              • Michel Posseth  [MCP]

                #8
                Re: object = nothing?

                Hello ,

                Yes ,, VB 2005 comes with Visual Studio 2005

                The using statement can only be used inside methods

                The using statement guarantees , that dispose is called after exiting the
                using block

                Public Sub setbigbold(ByVa l c As Control)
                Using nf As New System.Drawing. Font("Arial", 12.0F, _
                System.Drawing. FontStyle.Bold)

                c.Font = nf
                c.Text = "This is 12-point Arial bold"
                End Using
                End Sub


                for more info about the using statement
                http://msdn2.microsoft.com/en-us/library/htd05whh.aspx

                hth

                Michel Posseth [MCP]

                <param@communit y.nospamschreef in bericht
                news:ejo3WNg9GH A.4552@TK2MSFTN GP05.phx.gbl...
                So forgive my ignorance here, but I am presuming VB 2005 comes with Visual
                Studio 2005 and .net 2.0 framework right? What does the Using statement
                do? Can I use it for both Public and Private variable declarations?
                >
                >
                "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
                news:OsjqzQc9GH A.4572@TK2MSFTN GP02.phx.gbl...
                >Hello ,,,
                >>
                >Well you have started something :-) ....... this has been discussed
                >manny times in this group and it always ends the same :-)
                >>
                >The below is my opinion , wich is formed by following a lot of these
                >discussions and how it is described in the manny books i own
                >>
                >The BIG NO NO is a bit odd , he would be right if he would have said
                >that it isn`t necesary and that you are typing a line to much
                >if your object is declared in method scope
                >>
                >but there are situations thinkable where it is valid to set a object to
                >Nothing ( however never absolut necesary , as it could have been in
                >VB6 )
                >>
                >so here are some scenario`s for you to rethink :
                >>
                >Example A
                >Private sub Useobject ()
                >dim foo as new Objfoo.lib
                >>
                >---- use the foo object
                >>
                >--- setting the foo object here to nothing is absolute not necesary
                >--- as soon as it runs out of scope it is marked for collection
                >end sub
                >>
                >Example B
                >>
                >Private sub Useobject ()
                >dim foo as new Objfoo.lib
                >>
                >for i as integer = 0 to 1000
                >>
                >if i < 500 then
                >---- use the foo object
                >else
                >---- stop using the foo object
                >end if
                >next
                >end sub
                >>
                >--- here is a situation where it is valid to set the foo object to
                >nothing in the else statement ( if i = 500 then foo=Nothing )
                >--- especially when it is a resource hungry object , However it is not
                >guaranteed that the object is collected at this point
                >end sub
                >>
                >Example C
                >The object is declared in class scope ( because you use it withevents or
                >whatever reasson you might have )
                >and for some reasson you can live without it , for a time and if needed
                >you can declare a new instance
                >i encountered this scenario myself in a remoting scenario wich used a
                >singleton
                >>
                >>
                >if you use VB.Net 2005 then learn yourself to use the using stetement (
                >it makes sure everything is nicely cleaned up , and as a bonus it makes
                >your code nicer readable )
                >>
                >example
                >>
                >Private sub Useobject ()
                >using foo as new Objfoo.lib
                >>
                >---- use the foo object
                >>
                >end using
                >end sub
                >>
                >>
                >As said the above is my own opinion without warranty`s especially about
                >example B i get a lot of nasty responses however it is a Balena example
                >written in the core reference guide of VB.net,, so i guess it should be a
                >valid situation .
                >>
                >I know this thread is going to explode ( as it normally does on this
                >subject ) with all people telling you something different , i would say
                >form your own opinion
                >by reading about the GC on MSDN then you will see what it does and what
                >it doesn`t , and especially this is verry important , setting a object to
                >Nothing does not mean that you will get your resources inmediatly back ,
                >however in resource hungry systems it might make a difference in the
                >right scenario .
                >>
                >regards
                >>
                >Michel Posseth [MCP]
                >>
                >>
                >>
                >>
                >>
                >>
                ><param@communi ty.nospamschree f in bericht
                >news:eGGwBDV9G HA.1172@TK2MSFT NGP03.phx.gbl.. .
                >>Hi all,
                >>>
                >>Coming from the good old VB6 days we were told to always destroy our
                >>objects as follows:-
                >>>
                >>Dim obj as New MyObject
                >>' do some work with obj
                >>obj = Nothing
                >>>
                >>I have been doing this in .Net also for quite a while now. The other day
                >>one of my peers mentioned that this is a BIG NO NO and that I should let
                >>the Garbage Collection process handle this for me.
                >>>
                >>Can someone share any insight on this?
                >>>
                >>TIA!
                >>>
                >>
                >>
                >
                >

                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: object = nothing?

                  "Michel Posseth [MCP]" <MSDN@posseth.c omschrieb:
                  The using statement guarantees , that dispose is called after exiting the
                  using block
                  >
                  Public Sub setbigbold(ByVa l c As Control)
                  Using nf As New System.Drawing. Font("Arial", 12.0F, _
                  System.Drawing. FontStyle.Bold)
                  >
                  c.Font = nf
                  c.Text = "This is 12-point Arial bold"
                  End Using
                  End Sub
                  Using 'Using' is a bad idea in the sample above because the control needs an
                  undisposed font even after setting the font!

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

                  Comment

                  • Jeffrey Tan[MSFT]

                    #10
                    Re: object = nothing?

                    Hi,

                    Yes, Herfried means that if the "obj" reference is a "local" variable, then
                    it is not necessary to set "obj" to nothing to help GC. This is because, as
                    a "local" variable(refere nce), "obj" will be go out of scope automatically.
                    So after exiting the method, it automatically has the same effect of
                    setting "obj" to nothing.("obj" is destroyed automatically, so it is
                    "nothing").

                    Anyway, I do not think setting "obj" to nothing is a "Big NO, NO". It will
                    not hurt performance much, but it is a good habit to "help" GC in certain
                    situation. Let's say that you have a private field of bitmap type(note, it
                    is not a "local" variable). If you are using this field to refer a large
                    bitmap, after you used it, it is a really good idea to set the field to
                    "nothing", this may help to tell GC that this large bitmap is not
                    referenced by any references, so it can be collected once the free memory
                    is low.

                    So this is not a big issue, you may continue to set all the unused
                    references to nothing. This may or may not help GC, but it will not hurt
                    performance much.

                    Hope this is clear.

                    Best regards,
                    Jeffrey Tan
                    Microsoft Online Community Support
                    =============== =============== =============== =====
                    Get notification to my posts through email? Please refer to
                    http://msdn.microsoft.com/subscripti...ult.aspx#notif
                    ications.

                    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
                    where an initial response from the community or a Microsoft Support
                    Engineer within 1 business day is acceptable. Please note that each follow
                    up response may take approximately 2 business days as the support
                    professional working with you may need further investigation to reach the
                    most efficient resolution. The offering is not appropriate for situations
                    that require urgent, real-time or phone-based interactions or complex
                    project analysis and dump analysis issues. Issues of this nature are best
                    handled working with a dedicated Microsoft Support Engineer by contacting
                    Microsoft Customer Support Services (CSS) at
                    http://msdn.microsoft.com/subscripti...t/default.aspx.
                    =============== =============== =============== =====
                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: object = nothing?

                      Michel,

                      What can be the reason that you set in your first exsample 2 the object
                      privat.

                      In both situations is their in my opinion (if you set them in your method at
                      the end to nothing) not any reason to do that, although it seems to be a
                      kind of obfuscating from some developers to set everything global.

                      Cor



                      Only the last one will stay if you don't set it to nothing.
                      "Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
                      news:OsjqzQc9GH A.4572@TK2MSFTN GP02.phx.gbl...
                      Hello ,,,
                      >
                      Well you have started something :-) ....... this has been discussed
                      manny times in this group and it always ends the same :-)
                      >
                      The below is my opinion , wich is formed by following a lot of these
                      discussions and how it is described in the manny books i own
                      >
                      The BIG NO NO is a bit odd , he would be right if he would have said
                      that it isn`t necesary and that you are typing a line to much
                      if your object is declared in method scope
                      >
                      but there are situations thinkable where it is valid to set a object to
                      Nothing ( however never absolut necesary , as it could have been in
                      VB6 )
                      >
                      so here are some scenario`s for you to rethink :
                      >
                      Example A
                      Private sub Useobject ()
                      dim foo as new Objfoo.lib
                      >
                      ---- use the foo object
                      >
                      --- setting the foo object here to nothing is absolute not necesary
                      --- as soon as it runs out of scope it is marked for collection
                      end sub
                      >
                      Example B
                      >
                      Private sub Useobject ()
                      dim foo as new Objfoo.lib
                      >
                      for i as integer = 0 to 1000
                      >
                      if i < 500 then
                      ---- use the foo object
                      else
                      ---- stop using the foo object
                      end if
                      next
                      end sub
                      >
                      --- here is a situation where it is valid to set the foo object to nothing
                      in the else statement ( if i = 500 then foo=Nothing )
                      --- especially when it is a resource hungry object , However it is not
                      guaranteed that the object is collected at this point
                      end sub
                      >
                      Example C
                      The object is declared in class scope ( because you use it withevents or
                      whatever reasson you might have )
                      and for some reasson you can live without it , for a time and if needed
                      you can declare a new instance
                      i encountered this scenario myself in a remoting scenario wich used a
                      singleton
                      >
                      >
                      if you use VB.Net 2005 then learn yourself to use the using stetement
                      ( it makes sure everything is nicely cleaned up , and as a bonus it makes
                      your code nicer readable )
                      >
                      example
                      >
                      Private sub Useobject ()
                      using foo as new Objfoo.lib
                      >
                      ---- use the foo object
                      >
                      end using
                      end sub
                      >
                      >
                      As said the above is my own opinion without warranty`s especially about
                      example B i get a lot of nasty responses however it is a Balena example
                      written in the core reference guide of VB.net,, so i guess it should be a
                      valid situation .
                      >
                      I know this thread is going to explode ( as it normally does on this
                      subject ) with all people telling you something different , i would say
                      form your own opinion
                      by reading about the GC on MSDN then you will see what it does and what
                      it doesn`t , and especially this is verry important , setting a object to
                      Nothing does not mean that you will get your resources inmediatly back ,
                      however in resource hungry systems it might make a difference in the right
                      scenario .
                      >
                      regards
                      >
                      Michel Posseth [MCP]
                      >
                      >
                      >
                      >
                      >
                      >
                      <param@communit y.nospamschreef in bericht
                      news:eGGwBDV9GH A.1172@TK2MSFTN GP03.phx.gbl...
                      >Hi all,
                      >>
                      >Coming from the good old VB6 days we were told to always destroy our
                      >objects as follows:-
                      >>
                      >Dim obj as New MyObject
                      >' do some work with obj
                      >obj = Nothing
                      >>
                      >I have been doing this in .Net also for quite a while now. The other day
                      >one of my peers mentioned that this is a BIG NO NO and that I should let
                      >the Garbage Collection process handle this for me.
                      >>
                      >Can someone share any insight on this?
                      >>
                      >TIA!
                      >>
                      >
                      >

                      Comment

                      • Michel Posseth  [MCP]

                        #12
                        Re: object = nothing?

                        Herfried ..
                        Using 'Using' is a bad idea in the sample above because the control needs
                        an undisposed font even after setting the font!


                        This example is copied and pasted from the MSDN url i provided to the TS

                        so i guess someone at MS has some work to do ??

                        scroll to the bottom of this URL to see the example that MS provides

                        http://msdn2.microsoft.com/en-us/library/htd05whh.aspx


                        regards

                        Michel Posseth [MCP]


                        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
                        news:%23if0xDh9 GHA.4644@TK2MSF TNGP04.phx.gbl. ..
                        "Michel Posseth [MCP]" <MSDN@posseth.c omschrieb:
                        >The using statement guarantees , that dispose is called after exiting
                        >the using block
                        >>
                        >Public Sub setbigbold(ByVa l c As Control)
                        > Using nf As New System.Drawing. Font("Arial", 12.0F, _
                        > System.Drawing. FontStyle.Bold)
                        >>
                        > c.Font = nf
                        > c.Text = "This is 12-point Arial bold"
                        > End Using
                        >End Sub
                        >
                        Using 'Using' is a bad idea in the sample above because the control needs
                        an undisposed font even after setting the font!
                        >
                        --
                        M S Herfried K. Wagner
                        M V P <URL:http://dotnet.mvps.org/>
                        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                        Comment

                        • Michel Posseth  [MCP]

                          #13
                          Re: object = nothing?

                          I did not set anny object private in my example code ,,,, i did discuss this
                          in my example C as a hypothese in this cas it might be valid if you use the
                          same object in multiple methods ( so the object is declared in the hole
                          class context ) if a certain codition is met then you might choose to
                          destroy the object explicit in my opinion this is a valid reasson to set a
                          object to Nothing


                          regards

                          Michel


                          "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschre ef in bericht
                          news:eMwP6el9GH A.3352@TK2MSFTN GP03.phx.gbl...
                          Michel,
                          >
                          What can be the reason that you set in your first exsample 2 the object
                          privat.
                          >
                          In both situations is their in my opinion (if you set them in your method
                          at the end to nothing) not any reason to do that, although it seems to be
                          a kind of obfuscating from some developers to set everything global.
                          >
                          Cor
                          >
                          >
                          >
                          Only the last one will stay if you don't set it to nothing.
                          "Michel Posseth [MCP]" <MSDN@posseth.c omschreef in bericht
                          news:OsjqzQc9GH A.4572@TK2MSFTN GP02.phx.gbl...
                          >Hello ,,,
                          >>
                          >Well you have started something :-) ....... this has been discussed
                          >manny times in this group and it always ends the same :-)
                          >>
                          >The below is my opinion , wich is formed by following a lot of these
                          >discussions and how it is described in the manny books i own
                          >>
                          >The BIG NO NO is a bit odd , he would be right if he would have said
                          >that it isn`t necesary and that you are typing a line to much
                          >if your object is declared in method scope
                          >>
                          >but there are situations thinkable where it is valid to set a object to
                          >Nothing ( however never absolut necesary , as it could have been in
                          >VB6 )
                          >>
                          >so here are some scenario`s for you to rethink :
                          >>
                          >Example A
                          >Private sub Useobject ()
                          >dim foo as new Objfoo.lib
                          >>
                          >---- use the foo object
                          >>
                          >--- setting the foo object here to nothing is absolute not necesary
                          >--- as soon as it runs out of scope it is marked for collection
                          >end sub
                          >>
                          >Example B
                          >>
                          >Private sub Useobject ()
                          >dim foo as new Objfoo.lib
                          >>
                          >for i as integer = 0 to 1000
                          >>
                          >if i < 500 then
                          >---- use the foo object
                          >else
                          >---- stop using the foo object
                          >end if
                          >next
                          >end sub
                          >>
                          >--- here is a situation where it is valid to set the foo object to
                          >nothing in the else statement ( if i = 500 then foo=Nothing )
                          >--- especially when it is a resource hungry object , However it is not
                          >guaranteed that the object is collected at this point
                          >end sub
                          >>
                          >Example C
                          >The object is declared in class scope ( because you use it withevents or
                          >whatever reasson you might have )
                          >and for some reasson you can live without it , for a time and if needed
                          >you can declare a new instance
                          >i encountered this scenario myself in a remoting scenario wich used a
                          >singleton
                          >>
                          >>
                          >if you use VB.Net 2005 then learn yourself to use the using stetement (
                          >it makes sure everything is nicely cleaned up , and as a bonus it makes
                          >your code nicer readable )
                          >>
                          >example
                          >>
                          >Private sub Useobject ()
                          >using foo as new Objfoo.lib
                          >>
                          >---- use the foo object
                          >>
                          >end using
                          >end sub
                          >>
                          >>
                          >As said the above is my own opinion without warranty`s especially about
                          >example B i get a lot of nasty responses however it is a Balena example
                          >written in the core reference guide of VB.net,, so i guess it should be a
                          >valid situation .
                          >>
                          >I know this thread is going to explode ( as it normally does on this
                          >subject ) with all people telling you something different , i would say
                          >form your own opinion
                          >by reading about the GC on MSDN then you will see what it does and what
                          >it doesn`t , and especially this is verry important , setting a object to
                          >Nothing does not mean that you will get your resources inmediatly back ,
                          >however in resource hungry systems it might make a difference in the
                          >right scenario .
                          >>
                          >regards
                          >>
                          >Michel Posseth [MCP]
                          >>
                          >>
                          >>
                          >>
                          >>
                          >>
                          ><param@communi ty.nospamschree f in bericht
                          >news:eGGwBDV9G HA.1172@TK2MSFT NGP03.phx.gbl.. .
                          >>Hi all,
                          >>>
                          >>Coming from the good old VB6 days we were told to always destroy our
                          >>objects as follows:-
                          >>>
                          >>Dim obj as New MyObject
                          >>' do some work with obj
                          >>obj = Nothing
                          >>>
                          >>I have been doing this in .Net also for quite a while now. The other day
                          >>one of my peers mentioned that this is a BIG NO NO and that I should let
                          >>the Garbage Collection process handle this for me.
                          >>>
                          >>Can someone share any insight on this?
                          >>>
                          >>TIA!
                          >>>
                          >>
                          >>
                          >
                          >

                          Comment

                          • M. Posseth

                            #14
                            Re: object = nothing?

                            Herfried ,

                            Curious as i am i have just tested the example provided by MSDN

                            so i threw a control in the method ,,, and ..... it worked as expected


                            Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                            System.EventArg s) Handles Button1.Click
                            setbigbold(Butt on1)
                            End Sub

                            Public Sub setbigbold(ByVa l c As Control)
                            Using nf As New System.Drawing. Font("Arial", 12.0F, _
                            System.Drawing. FontStyle.Bold)

                            c.Font = nf
                            c.Text = "This is 12-point Arial bold"
                            End Using
                            End Sub


                            regards

                            Michel Posseth [MCP]



                            "Michel Posseth [MCP]" wrote:
                            Herfried ..
                            >
                            Using 'Using' is a bad idea in the sample above because the control needs
                            an undisposed font even after setting the font!
                            >
                            >
                            >
                            This example is copied and pasted from the MSDN url i provided to the TS
                            >
                            so i guess someone at MS has some work to do ??
                            >
                            scroll to the bottom of this URL to see the example that MS provides
                            >
                            http://msdn2.microsoft.com/en-us/library/htd05whh.aspx
                            >
                            >
                            regards
                            >
                            Michel Posseth [MCP]
                            >
                            >
                            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
                            news:%23if0xDh9 GHA.4644@TK2MSF TNGP04.phx.gbl. ..
                            "Michel Posseth [MCP]" <MSDN@posseth.c omschrieb:
                            The using statement guarantees , that dispose is called after exiting
                            the using block
                            >
                            Public Sub setbigbold(ByVa l c As Control)
                            Using nf As New System.Drawing. Font("Arial", 12.0F, _
                            System.Drawing. FontStyle.Bold)
                            >
                            c.Font = nf
                            c.Text = "This is 12-point Arial bold"
                            End Using
                            End Sub
                            Using 'Using' is a bad idea in the sample above because the control needs
                            an undisposed font even after setting the font!

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

                            Comment

                            Working...