3

I wanted to make a helper class for my MySql wrapper. The idea is that the methods that encapsulate mysql command construction and execution have an optional MySqlConnection as an argument. If a specific connection is passed, it uses that, if not, it creates one and disposes of it once done. To save 4 lines off every method, I could use this class in the using block and pass the optional argument as a construction parameter. Anyway, heres the class:

Public Class DynaConnection
    Implements IDisposable

    Public Dynamic As Boolean
    Public Connection As MySqlConnection
    Public Sub New(Connection As MySqlConnection)
        If Connection Is Nothing Then
            Dynamic = True
            Me.Connection = Connect()
        Else
            Dynamic = False
        End If
    End Sub
    Public Shared Widening Operator CType(ByVal Connection As DynaConnection) As MySqlConnection
        Return Connection.Connection
    End Operator

    Public Sub Dispose() Implements IDisposable.Dispose
        If Dynamic Then
            Connection.Close()
            Connection.Dispose()
        End If
        GC.SuppressFinalize(Me)
    End Sub
End Class

When I first wrote the letters "Implements IDisposable" though, a whole wall of code jumped into the class. I looked at msdn to see whats what but over there was an even longer bunch of code on how to "properly" implement IDisposable.

From what I remember from writing simple IDisposable classes before, what I've done in the class above should suffice. Has something changed?

1
  • 1
    When I first wrote the letters "Implements IDisposable" though, a whole wall of code jumped into the class - yes, that wall of code is the correct way to implement the IDisposable pattern in VB.NET. Keep this (very very little) wall of code. It has two comments in it - "dispose this kind of objects here" and "dispose that kind of objects here". Put your calls to Dispose in these designated places. Commented Sep 13, 2015 at 9:55

1 Answer 1

6

This is that "wall of code" with some additional comments.

' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            ' TODO: dispose managed state (managed objects).

            'If your class holds references to other .NET objects 
            'that themselves implement IDisposable then you should implement IDisposable 
            'and call their Dispose method in yours

        End If

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.

        'If you're holding any OS resources, e.g. file or image handles, 
        'then you should release them. That will be a rare thing for most people and can be pretty much ignored

        ' TODO: set large fields to null.
        'If any of your fields may refer to objects that occupy 
        'a large amount of memory then those fields should be set to Nothing

    End If
    Me.disposedValue = True
End Sub

' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'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

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

I agree with GSeng, this is the correct way to implement IDisposable.

Sign up to request clarification or add additional context in comments.

2 Comments

I have never understood why the boilerplate code puts the suggestion to set large fields to null outside the If disposing block. If disposing is false, the method is being called from the finalizer and the fields are (or about to be) null anyway.
@Demodave - what 'it' for a Form. Forms already have a .Dispose method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.