469,919 Members | 2,135 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,919 developers. It's quick & easy.

Question: Dispose with shared members

I'm having this class

Public Class TestClass
Implements IDisposable
Dim Conn As SqlConnection
Public Sub New(ByVal connectionString As String)
Conn = New SqlConnection(connectionString)
End Sub
Public Function GetDateTime() As DateTime
Dim Cmd As SqlCommand = New SqlCommand("SELECT
GETDATE()", Conn)
If Conn.State <ConnectionState.Open Then Conn.Open()
Dim Today As DateTime = Cmd.ExecuteScalar()
Return Today
End Function
Private disposedValue As Boolean = False ' To
detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when
explicitly called
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' 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
disposing As Boolean) above.
End Sub
#End Region
End Class
I'm using it through Namespace "My" like this
Namespace My
Public Module MyTestClass
Private _TestClass As New ThreadSafeObjectProvider(Of
Public ReadOnly Property TestClass() As
Return _TestClass.GetInstance()
End Get
End Property
End Module
End Namespace
Is it correct to release unmanaged resources like Conn in TestClass by
calling My.TestClass.Dispose() ? When i'm using If Not My.TestClass Is
Nothing Then My.TestClass.Dispose a new instance of the class is
created and then disposed. Is there anything else to do to avoid this ?

is this correct ?
thanks in advance for your answers :)

Sep 18 '06 #1
0 746

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Ken Durden | last post: by
5 posts views Thread by Mathias L. | last post: by
reply views Thread by D. André Dhondt | last post: by
4 posts views Thread by phl | last post: by
9 posts views Thread by Mike Hofer | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.