I should add that these rules apply to objects that you create, explicitly
or implicitly. Objects that you "own"
Disposable Objects that are passed to you as a parameter of a method (such
as the Graphics object on the Paint event) should not have their disposed
method call, as the system calls it as part of the method that raises the
event.
Objects that something else "owns" should normally be disposed of by the
"owning" object.
Thanks Cor, I need to add designed Components to my list.
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
| Geoff,
| In addition to the other comments, these are the rules I use to decide if
I
| should call Dispose or not:
|
| When to call Dispose?
|
| * Call Dispose when the type itself implements IDisposable
|
| * Call Dispose when the type or one of its base classes *overrides*
| Dispose(Boolean) if the class inherits from
System.ComponentModel.Component
| or System.ComponentModel.MarshalByValueComponent
|
| * Do not explicitly call Dispose on classes deriving from
| System.Windows.Forms.Control for instances placed on a
| System.Windows.Forms.Form as Form will implicitly dispose of them when the
| form is Disposed
|
| * Call Dispose on System.Windows.Forms.Form objects when Form.ShowDialog
is
| used.
|
| * Do not explicitly call Dispose on System.Windows.Forms.Form objects if
| Form.Show is used as Dispose will be implicitly called when the form is
| closed
|
| * Do not explicitly call Dispose on classes deriving from
| System.Web.UI.Control as it will be implicitly called as part of the
normal
| ASP.NET page processing
|
| I consider the second rule controversial as it relies on using ILDASM or
| Reflector to find out implementation details of a class. Its meant for
| classes such as DataSet, that have an inherited Dispose, but Dispose
doesn't
| really do anything.
|
| These rules are based on private discussions with other MVPs & discussions
| held in the newsgroups earlier in 2005.
|
|
| VB 2005 (.NET 2.0) introduces the Using statement that simplifies calling
| Dispose.
|
| ' VB 2005 syntax
| Using dialog As New OptionsDialog
| dialog.ShowDialog(Me)
| End Using
|
| ' VB 2002 & 2003 syntax
| Dim dialog As New OptionsDialog
| Try
| dialog.ShowDialog(Me)
| Finally
| ' assuming that dialog is not nothing...
| dialog.Dispose()
| End Finally
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley -
http://www.tsbradley.net
|
|
| "Geoff Callaghan" <gc********@trakeng.com> wrote in message
| news:uo**************@tk2msftngp13.phx.gbl...
||I have several functions that require doing a New on a local object. If
the
|| object is local to a sub or function, do I need to dispose of it, or will
|| it just go away like any other local variable?
||
|| Geoff Callaghan
||
||
|
|