473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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()
Cmd.Dispose()
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
Conn.Dispose()
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
Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
I'm using it through Namespace "My" like this
Namespace My
<Global.Microsoft.VisualBasic.HideModuleName()_
Public Module MyTestClass
Private _TestClass As New ThreadSafeObjectProvider(Of
Global.TestClass)
Public ReadOnly Property TestClass() As
Global.TestClass
Get
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 862

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
Hi. I don't have a problem per se, I was just wondering if anyone can offer some opinions about the best way to go about creating and disposing of a Singleton class. I have a class (handling...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
5
by: Mathias L. | last post by:
I have two questions for which I couldnt find answer: If I programaticaly close DialogForm (calling Close()), is it enough or do I have to dispose it as MS.NET help says? Also, in overriden...
0
by: D. André Dhondt | last post by:
How do I dispose of a shared object instance only once--after all consumers of it have been disposed of? More detail: I have a trace class that I invoke as a shared instance in the business...
6
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
0
by: demon | last post by:
I'm having this class Public Class TestClass Implements IDisposable Dim Conn As SqlConnection Public Sub New(ByVal connectionString As String)
9
by: Mike Hofer | last post by:
In a large application I'm working on (ASP.NET 1.1, VS2003), we have a base class that wraps stored procedures. Essentially, this base class (StoredProcedureBase) encapsulates the code to set up...
5
by: Russell Mangel | last post by:
Here is something much closer to what I was trying to do. Thanks to Arne, and Peter and all who helped. Sorry I didn't explain my question better. Russell Mangel Las Vegas, NV // Begin...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.