what is the use of this Dispose in program

abhishek.chins

New member
Joined
Jan 28, 2008
Messages
1
Programming Experience
Beginner
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
 
The Dispose method releases managed and unmanaged resources when the object is no longer needed. This allows the system to reclaim and reuse valuable resources. An unmanaged resource is something acquired from the system itself, like a window handle or image handle. A managed resource is an object that itself holds an unmanaged resource or another managed resource.

An example is when you have no further need of an Image object you should Dispose it so it can release its image handle back to the system. If you don't then the system will have fewer resources to allocate and may end up performing more poorly as a result.

If you want to know more then read about the IDisposable interface on MSDN. There's plenty of information on object disposal besides.
 
Implementing an interface is a contract between developer and user of class. When you implement IDisposable in a class any user of that class can be sure that if they call the Dispose method all resources taken by the class is released - that is, if you have written the class properly. In the case of interface IDisposable specifically you just have to learn that this is the single "cleanup" interface used by all classes (- the exception is if developer of class for example tell you that using Close method is also good for final call and cleanup). So if you create an instance of a class in code and you see it has a Dispose method you would call it when you're finished with the object to let this class do any cleanups necessary. Why many beginners forget this is because this is not done manually for components/controls added to form, the forms generated code automatically dispose all controls and their childs when closing. Also, you don't get an error if you don't dispose in most cases, and eventually memory will be reclaimed, so for beginners this topic may be transparent and of less importance than 'managing to get the job done at all'.
There also exist a code structure in VB that takes advantage of this contract for disposal, the Using code block. Check with documentation to see how this work, and may ease the code writing for you in regard of cleanup code.
 
Back
Top