basic question...destructors confusion

M

Mobileboy36

Public sub New()



End sub



How to write a destructor in VB.NET?



Protected Overrides Sub Finalize()

???

End Sub





Why you have to call class.dispose than in some cases.

When I implement Finialize, do I have to implement Dispose too?
 
I

Ilya Tumanov [MS]

There are no destructors, there are finalizers (as your code shows). The
difference is what destructor in C++ is always called before class is
destroyed and finalizers might never be called and even if they are called
you can never predict when that would happened. That is why you have to call
(or implement) Dispose() instead of using finalizers.



If your class uses native resources then you have to implement IDisposable
interface correctly and call it from finalizer with "false" as argument:



Protected Overrides Sub Finalize()
Dispose(false)

End Sub

That argument means "Do NOT touch any managed objects" because they might be
already collected.



If you don't use any native resources in your class then you don't need to
implement IDispossable and you don't need finalizer.



See this for more info:



http://weblogs.asp.net/cnagel/archive/2005/01/23/359037.aspx



--
Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and confers no rights.



*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
G

Guest

I *always* implement IDisposable even if I am not using unmanaged resources
so that I can use the using statement in C# and the Dispose method will clean
up any managed or unmanaged (if any) resources.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top