473,386 Members | 1,647 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,386 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 1015

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...
2
by: Fekkai | last post by:
I have a question about threads: In the TCPClient help page I find the following explanation "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.