where to call savetoxml to run on object destruction

  • Thread starter Thread starter deciacco
  • Start date Start date
D

deciacco

I'm using an object that has a dataset within it.
When the object is instantiated I populate the dataset from xml files.
I want the object to save the dataset back to the xml file before it is
destroyed, but I'm not sure when to call this routine. Should I call it
from the object's destructor or should it implement the IDisposable
interface?

Thanks!
 
deciacco,

You should implement the IDisposable interface. In the Dispose method,
you should write the file.

The suggested pattern for implementing IDispose has the finalizer run
the same code (with a variation). For this kind of activity, I would say to
not do this, as it appears that you are looking for specific lifetime
management. Since finalization is non-deterministic, calling the code from
there would be a bad idea.
 
Thanks for the reply,

Forgive me, but I'm pretty new to C# and I do not understand the second
part.

Are you saying that if I do it, I should use IDisposable, but this isn't
a good way either?
 
No, I am saying you should implement IDisposable, but don't place the
logic in the finalizer to write the file.
 
Back
Top