Brano,
Review this snippet and see if you can make use of the concepts within.
Public MustInherit Class CUniDataBase
Implements IDisposable
Private disposedValue As Boolean = False ' To detect redundant calls
Private myLegacy As Object ' Pointer to legacy database
Private myConnection As String ' Holds connection information
Private isInitialized As Boolean ' State flag of the unmanaged
object
Public Sub New(ByVal ConnectionString As String)
myConnection = ConnectionString
'myLegacy is not init'ed yet
isInitialized = False
End Sub
Public Sub Connect()
If Not isInitialized Then
myLegacy = CreateObject("SomeContainer.SomeObject")
myLegacy.Connection = myConnection
myLegacy.Connect()
isInitialized = True
End If
End Sub
Public Sub Disconnect()
If isInitialized AndAlso Not myLegacy Is Nothing Then
'Free up pointers
myLegacy.Disconnect()
End If
myLegacy = Nothing
isInitialized = False
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
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
Me.Disconnect()
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class
"Brano" <bp****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi, It doesnt actually...
But I can access this class and reprogram it.
I have tried to add the microsoft code to do that which is:
Public MustInherit Class CUniDataBase
Implements IDisposable
' NB - ALWAYS CLOSE CONNECTION ON EXIT
' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private component As Component
' Track whether Dispose has been called.
Private disposed As Boolean = False
' The class constructor.
Public Sub New(ByVal handle As IntPtr)
Me.handle = handle
End Sub
' Implement IDisposable.
' Do not make this method virtual.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' This object will be cleaned up by the Dispose method.
' Therefore, you should call GC.SupressFinalize to
' take this object off the finalization queue
' and prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub
' Dispose(bool disposing) executes in two distinct scenarios.
' If disposing equals 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.
Private 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.
component.Dispose()
End If
' Call the appropriate methods to clean up
' unmanaged resources here.
' If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
End If
disposed = True
End Sub
' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel3 2")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As
[Boolean]
End Function
' This finalizer will run only if the Dispose method
' does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this
class.
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)
MyBase.Finalize()
End Sub
But if I cann the dispose now I get Object reference not set to an
instance of an object error...
;o(