Implements IDisposable

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

I was recommended to implement Idisposable in a class
I'm not getting the syntax right and don't find an example in help i can
follow
is there a simplified example or can someone see what i should do here?

I have a utility class that gets a reference to an object(acad application)
The main class instantiates the utility class, the utility class gets the
object(acad) and passes to the main class to use

when the main class is done, what is the correct process to tear down the
references to the object?

the main class init sub is as follows:
Function Init() As Boolean
m_Util = New MCSUtil(True) 'create utility class
m_Util.Init2(True)

If m_acad Is Nothing Then
m_acad = m_Util.ACAD 'get reference to object for internal use
End If

If m_acad Is Nothing Then
MsgBox("Can not get AutoCAD instance.", MsgBoxStyle.Exclamation,
"")
Exit Function
End If

m_acadDoc = m_acad.ActiveDocument

If m_acadDoc Is Nothing Then
MsgBox("No active document.", MsgBoxStyle.Information, "")
Exit Function
End If

'if we're still here all is well
Init = True
End Function

in the utility class:
Function Init2(ByVal bDebug As Boolean) As Boolean
m_Debug = bDebug
Try
m_AcadApp =
DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication,
AcadApplication)
m_acadDoc = m_AcadApp.ActiveDocument
Return True

Catch ex As Exception
Return False
End Try
End Function
'the variable declaration in utility class:
Private m_AcadApp As Autodesk.AutoCAD.Interop.AcadApplication = Nothing



which class should implement iDisposable, and how is that sub Dispose
written?

I tried putting in the Utility class:

<System.Runtime.InteropServices.ComVisible(False)> Public Class MCSUtil
Implements IDisposable

Private m_acadDoc As AcadDocument
Private m_AcadApp As Autodesk.AutoCAD.Interop.AcadApplication = Nothing
Private m_Debug As Boolean

Public Overloads Sub Dispose() Implements IDisposable.Dispose

Dispose()
m_AcadApp = Nothing
m_acadDoc = Nothing
GC.SuppressFinalize(Me)

End Sub

i found this through the help

'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

but when i try to duplicate thta in my code, the Dispose() method takes no
args so i get a compile error

any help?

thanks

mark
 
Hi,

The most easy way to implement IDisposable in a class, is to do Add New Item
and get the component template. That one has a complete implementation of
IDisposable (as code so you can even copy it).

Be aware that Idisposable is around disposing the unmanaged resources from
an object. It has not direct to do with disposing (releasing, finalizing,
deconstructing) an object itself.

About that is very much misunderstanding because some think it is the later.
What is of course impossible, you can not let something kill itself.

The standard classes which don't implement IDisposable anymore, become more
and more (like WPF).

Cor
 
Am 14.07.2010 20:54, schrieb mp:
I was recommended to implement Idisposable in a class
I'm not getting the syntax right and don't find an example in help i can
follow
is there a simplified example or can someone see what i should do here?

Cor said it. In my words:
If your class neither owns disposable objects nor created system
ressources, there is nothing to dispose and it doesn't need a Dispose
method.
 
Armin Zingler said:
Am 14.07.2010 20:54, schrieb mp:

Cor said it. In my words:
If your class neither owns disposable objects nor created system
ressources, there is nothing to dispose and it doesn't need a Dispose
method.

my class created a reference to an autocad.application object and it's
associated .Activedocument object
I thought my releasing those may be a solution to a problem with closing
acad after my app runs (in the acad application process space)
i'm not sure it is the solution, but trying whatever I can thinkof to fix
the problem
thanks
mark
 
Yes,

Armin did not say "you must not do it".

We can not see how you build your class and we don't know in what way you
are wrapping, so I would in your case use that component (class) as the base
for the class.

I gave not direct the same answer like Armin does now, because I (I suppose
we) are not aware if that ACAD part needs to be disposed.

Cor
 
Not even gibberish

The last sentence

I did not give direct the same answer like Armin did, because I don't know
if that ACAD component has parts, which needs to be disposed.

Cor
 
Back
Top