How to know when a Class/Object is Disposed/Finalized/ = Nothing

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

    #1

    How to know when a Class/Object is Disposed/Finalized/ = Nothing

    Hi,

    I have made some classes, but when they are Finalized/Disposed or simply
    "MyClass = Nothing", I want to trigger this action, and ask to save unsaved
    changes.

    How should I do this? I'm kind of testing with a Base-Class from which all
    my classes inherit to override the Finalize-method, but it doesn't really
    work... :-(

    Any help our hints would be really appreciated!

    Thanks a lot in advance,

    Pieter



  • Ken Tucker [MVP]

    #2
    Re: How to know when a Class/Object is Disposed/Finalized/ = Nothing

    Hi,

    Try having your class implement Idisposable that will add a dispose
    method where you can clean things up. I dont think setting the class to
    nothing will trigger dispose. Try using the myclass.dispose instead of
    myclass=nothing

    Public Class test

    Implements IDisposable

    Public Sub Dispose() Implements System.IDisposa ble.Dispose

    MessageBox.Show ("Disposing" )

    End Sub

    End Class



    Ken

    -----------------------

    "DraguVaso" <pietercoucke@h otmail.com> wrote in message
    news:%23vn5PiK0 FHA.1028@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hi,
    >
    > I have made some classes, but when they are Finalized/Disposed or simply
    > "MyClass = Nothing", I want to trigger this action, and ask to save
    > unsaved changes.
    >
    > How should I do this? I'm kind of testing with a Base-Class from which all
    > my classes inherit to override the Finalize-method, but it doesn't really
    > work... :-(
    >
    > Any help our hints would be really appreciated!
    >
    > Thanks a lot in advance,
    >
    > Pieter
    >
    >
    >[/color]


    Comment

    • Kevin Spencer

      #3
      Re: How to know when a Class/Object is Disposed/Finalized/ = Nothing

      Hi Pieter,

      Check out the following SDK reference on the IDisposable Interface. It
      includes (int the Remarks section) good advice and an excellent sample of
      the way you ought to implement the Dispose method while avoiding the types
      of problems you're describing.

      http://msdn.microsoft.com/library/de...classtopic.asp

      --
      HTH,

      Kevin Spencer
      Microsoft MVP
      ..Net Developer
      Ambiguity has a certain quality to it.

      "DraguVaso" <pietercoucke@h otmail.com> wrote in message
      news:%23vn5PiK0 FHA.1028@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Hi,
      >
      > I have made some classes, but when they are Finalized/Disposed or simply
      > "MyClass = Nothing", I want to trigger this action, and ask to save
      > unsaved changes.
      >
      > How should I do this? I'm kind of testing with a Base-Class from which all
      > my classes inherit to override the Finalize-method, but it doesn't really
      > work... :-(
      >
      > Any help our hints would be really appreciated!
      >
      > Thanks a lot in advance,
      >
      > Pieter
      >
      >
      >[/color]


      Comment

      • Phill.  W

        #4
        Re: How to know when a Class/Object is Disposed/Finalized/ = Nothing

        "DraguVaso" <pietercoucke@h otmail.com> wrote in message
        news:%23vn5PiK0 FHA.1028@TK2MSF TNGP12.phx.gbl. ..
        [color=blue]
        > I have made some classes, but when they are Finalized/Disposed
        > or simply "MyClass = Nothing", I want to trigger this action, and
        > ask to save unsaved changes.[/color]

        Finalizing/Disposing are definitely not the same as setting a
        Variable to Nothing.

        If you have code that you want to be called when the object is
        "no longer needed", have the class implement IDisposable and
        put the code into your Dispose method (have a look at the
        Dispose code written into any Windows Forms you create).

        It is /convention/ that when discarding an object that /has/ a
        Dispose method, the caller /should/ do so (sadly, this isn't
        actually /enforced/ anywhere).

        Also, with regards this bit ...

        "ask to save unsaved changes"

        .... spare a thought for /how/ you intend to do this.
        The quick answer is to pop up a MessageBox - but this could
        leave you a bit stuck further down the line if you want to, say,
        reuse these classes in a Windows Service; these don't like
        MessageBox's very much at all.

        HTH,
        Phill W.


        Comment

        • branco.medeiros@gmail.com

          #5
          Re: How to know when a Class/Object is Disposed/Finalized/ = Nothing


          DraguVaso wrote:
          <snip>[color=blue]
          > I have made some classes, but when they are Finalized/Disposed or simply
          > "MyClass = Nothing", I want to trigger this action, and ask to save unsaved
          > changes.
          >
          > How should I do this? I'm kind of testing with a Base-Class from which all
          > my classes inherit to override the Finalize-method, but it doesn't really
          > work... :-([/color]
          <snip>

          Alas, you're asking for deterministic finalization, and this is not
          provided in .Net. The closest to this is an Open/Close pattern: you
          implement these methods and the clients (users of your class) must call
          them at the appropriate times.

          This way you may have some tracking logic inside your class (reference
          counting, for exemple), that would detect the last 'Close' and take
          action if the data wasn't saved (raise an event, for example).

          Hope this helps...

          Regards,

          Branco.

          Comment

          • DraguVaso

            #6
            Re: How to know when a Class/Object is Disposed/Finalized/ = Nothing

            ok, thanks a lot for the help guys!

            Pieter

            <branco.medeiro s@gmail.com> wrote in message
            news:1129322404 .851853.54470@g 44g2000cwa.goog legroups.com...[color=blue]
            >
            > DraguVaso wrote:
            > <snip>[color=green]
            >> I have made some classes, but when they are Finalized/Disposed or simply
            >> "MyClass = Nothing", I want to trigger this action, and ask to save
            >> unsaved
            >> changes.
            >>
            >> How should I do this? I'm kind of testing with a Base-Class from which
            >> all
            >> my classes inherit to override the Finalize-method, but it doesn't really
            >> work... :-([/color]
            > <snip>
            >
            > Alas, you're asking for deterministic finalization, and this is not
            > provided in .Net. The closest to this is an Open/Close pattern: you
            > implement these methods and the clients (users of your class) must call
            > them at the appropriate times.
            >
            > This way you may have some tracking logic inside your class (reference
            > counting, for exemple), that would detect the last 'Close' and take
            > action if the data wasn't saved (raise an event, for example).
            >
            > Hope this helps...
            >
            > Regards,
            >
            > Branco.
            >[/color]


            Comment

            Working...