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

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

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
 
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.IDisposable.Dispose

MessageBox.Show("Disposing")

End Sub

End Class



Ken
 
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.

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.
 
DraguVaso wrote:
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... :-(
<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.
 
ok, thanks a lot for the help guys!

Pieter

DraguVaso wrote:

<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.
 
Back
Top