Destroy on request

  • Thread starter Thread starter ZippyV
  • Start date Start date
Z

ZippyV

Hello everybody,
I've made a simple class that reads an xml-file with some methods to delete
a row, add a row and to get the amount of lines in the dataset:

public class bugreports
private dsbugreport as new dataset

public sub new()
dsbugreport.readxmlscheme(...)
dsbugreport.readxml(...)
end sub

public sub insertreport()
...
dsbugreport.tables(0).rows.add(...)
end sub

public function viewreports()
return dsbugreport
end function

protected overrides sub finalize()
dsbugreport.writexml(...)
end sub
end public class

My problem is that I need to call the finalize sub when I want it (most of
the times at the end of the sub that called my class) and not the garbage
collection. I tried setting the 'bugreports = nothing' but that didn't call
the finalize sub.
The only solution I can think of is putting the 'dsbugreport.writexml(...)'
line in every sub/function where I change something in the dataset, but is
this the best method?
 
ZippyV,
The only solution I can think of is putting the 'dsbugreport.writexml(...)'
line in every sub/function where I change something in the dataset, but is
this the best method?
If you want to be certain that the file is always up to date, this would be
the "best" method!

An alternative would to be implement the Disposable/Finalization pattern
see:

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

Which then leaves it to the developer using your class to call Dispose when
they are done with the class.

I would be cautious about referring to the dsbugreport variable in the
Finalize method, as it may have been garbage collection already...

BTW: In your example I would consider having a Close method that Implements
IDisposable.Dispose, as that may be more "clear" to the developer using the
class:
public class bugreports
Implements IDisposable

Public Sub Close() Implements IDisposable.Dispose
...
End Sub

End Class

Hope this helps
Jay
 
Back
Top