CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Framework Design, Agile Coach, President Oklahoma City Developers Group, Microsoft MVP C#, TDD, Continuous Integration, Patterns and Practices, Domain Driven Design, Speaker, VB.Net, C# and Sql Server

Visual Studio 2005 IDE Automatically Produces Code to Implement IDisposable

Here’s a neat little thing about Visual Studio 2005 IDE for Visual Basic.Net.  When you implement the IDisposable interface, the following code is automatically created for you.  This is different from other interfaces in that only the code stubs/shell (empty methods) are produced for other interfaces.  The auto-cdoe for IDisposable actually produces the code needed for the entire implementation except for the actual cleaning up of resources.  The private fields, Public Dispose Sub and Finalize come complete with no needed modifications so that nothing gets left out.  However, if you forget to clean up a managed or unmanaged resource in the Private Dispose(bool) method, that's your own fault :). Update: As Blair mentioned in the comments, for C# IDE you can right click on the IDisposable term and pick if you want to explicitly or implictly fill in the stubs for the interface.

VB.Net 2.0 in 2005 IDE IDisposable implementation

 Public Class MyClass
    Implements IDisposable

    Private disposed As Boolean = False

    ' IDisposable
    Private Overloads Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposed Then
            If disposing Then
                ' TODO: put code to dispose managed resources
            End If

            ' TODO: put code to free unmanaged resources here
        End If
        Me.disposed = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Protected Overrides Sub Finalize()
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(False)
        MyBase.Finalize()
    End Sub
#End Region

End Class



Comments

Raymond Lewallen said:

Thanks for the tip Blair. I didn't play around with it too much in C#.
# May 11, 2005 8:54 AM

Christopher Steen said:

Link Listing - May 11, 2005
# May 11, 2005 9:06 PM

Hendy Irawan said:

I think this can also be implemented as a mixin (as in Ruby) for the IDisposable interface.
Is there such thing as mixins for .NET ?
# May 16, 2005 11:43 AM

Adam Volk said:

What clean up code is needed? Could you give me an example? Everyone's help file says, "put clean up code here." But no one tells you what the clean up code should look like.

My email address is Adam.Volk@Consultant.volvo.com

Here is the class I wrote that I'm trying to clean up:




Public Class truck_entry

'Public Class BaseResource
Implements IDisposable
' Pointer to an external unmanaged resource.
Private handle As IntPtr

' Track whether Dispose has been called.
Private disposed As Boolean = False

' Other managed resource this class uses.
' Private Components As Component
Private Line_Set_Str As String
Private Location_Str As String
Private Worker_1_Str As String
Private Worker_2_Str As String
Private Alert_Str As String
Private Category_Str As String
Private Model_Str As String
Private Time_Date As Date
Private Due_time_Dbl As Double
Private Comment_Str As String
Private I As Int16


' Constructor for the BaseResource Object.
Public Sub New()
' Insert appropriate constructor code here.
End Sub

' Implement IDisposable.
' Do not make this method Overridable.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' Take yourself off of the finalization queue
' to prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub

' Dispose(disposing As Boolean) executes in two distinct scenarios.
' If disposing is true, the method has been called directly
' or indirectly by a user's code. Managed and unmanaged resources
' can be disposed.
' If disposing equals false, the method has been called by the runtime
' from inside the finalizer and you should not reference other
' objects. Only unmanaged resources can be disposed.
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not (Me.disposed) Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If (disposing) Then
' Dispose managed resources.
'Components.Dispose()

End If
' Release unmanaged resources. If disposing is false,
' only the following code is executed.
'CloseHandle(handle)
handle = IntPtr.Zero
' Note that this is not thread safe.
' Another thread could start disposing the object
' after the managed resources are disposed,
' but before the disposed flag is set to true.
' If thread safety is necessary, it must be
' implemented by the client.
End If
Me.disposed = True
End Sub

' This Finalize method will run only if the
' Dispose method does not get called.
' By default, methods are NotOverridable.
' This prevents a derived class from overriding this method.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(False)
End Sub

' Allow your Dispose method to be called multiple times,
' but throw an exception if the object has been disposed.
' Whenever you do something with this class,
' check to see if it has been disposed.
Public Sub DoSomething()
If Me.disposed Then
'Throw New ObjectDisposedException
MsgBox("Dispose Called Too Often")
End If
End Sub

Public Property ID() As Int16
Get
Return I
End Get
Set(ByVal Value As Int16)
I = Value
End Set
End Property

Public Property Line_Set() As String
Get
Return Line_Set_Str
End Get
Set(ByVal Value As String)
Line_Set_Str = Value
End Set
End Property

Public Property Location() As String
Get
Return Location_Str
End Get
Set(ByVal Value As String)
Location_Str = Value
End Set
End Property

Public Property Worker_1() As String
Get
Return Worker_1_Str
End Get
Set(ByVal Value As String)
Worker_1_Str = Value
End Set
End Property

Public Property Worker_2() As String
Get
Return Worker_2_Str
End Get
Set(ByVal Value As String)
Worker_2_Str = Value
End Set
End Property

Public Property Alert() As String
Get
Return Alert_Str
End Get
Set(ByVal Value As String)
Alert_Str = Value
End Set
End Property

Public Property Category() As String
Get
Return Category_Str
End Get
Set(ByVal Value As String)
Category_Str = Value
End Set
End Property

Public Property Model() As String
Get
Return Model_Str
End Get
Set(ByVal Value As String)
Model_Str = Value
End Set
End Property

Public Property Time() As Date
Get
Return Time_Date
End Get
Set(ByVal Value As Date)
Time_Date = Value
End Set
End Property

Public Property Due_time() As Double
Get
Return Due_time_Dbl
End Get
Set(ByVal Value As Double)
Due_time_Dbl = Value
End Set
End Property

Public Property Comment() As String
Get
Return Comment_Str
End Get
Set(ByVal Value As String)
Comment_Str = Value
End Set
End Property

'Public Sub dispose()
' GC.Collect()
'End Sub

Public Sub Clear()
Line_Set = ""
Model = ""
Category = ""
Location = ""
Worker_1 = ""
Worker_2 = ""
Alert = ""
Due_time = 0
'time = ##
Comment = ""
End Sub

End Class


# July 21, 2005 1:39 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!

Our Sponsors

Free Tech Publications