Reputation: 129
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
Can anybody please tell me why we use this in designer.vb
?
Upvotes: 1
Views: 4219
Reputation: 9021
Dispose
is used to free resources that are not managed by the runtime (unmanaged resources). This include files, streams, fonts, etc. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.
In your code, the base class's Dispose
method is overridden by the child class implementation, hence the overriden
keyword the call to Mybase.Dispose
. The base class is IContainer
and the child class is Form
. The Dispose
method is available in the IDisposable
interface.
In designer.vb
, this auto generated code serves to call Dispose
on the components of the form when Dispose
is called on the form i.e. when the form is being disposed, dispose its components as well.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
' If Dispose() has been called explicitly free both managed and
' unmanaged resources (disposing = true)
' and there are components in the form
If disposing AndAlso components IsNot Nothing Then
' Free the resources used by components
components.Dispose()
End If
Finally
' Once done disposing the current form's resources,
' dispose the resources held by its base class
' Finally clause executes this code block (to dispose base class)
' whether or not an exception has been encountered while
' disposing the resources in the form
MyBase.Dispose(disposing)
End Try
More information
Upvotes: 3