Article ID: 315528 - View products that this article applies to.
This article was previously published under Q315528
When you author a class that extends a base class, you need to somehow handle the release of allocated resources. To do this, the Dispose method from the base class should be overridden in the derived classes. This article discusses common problems encountered in this scenario, how to properly override the Dispose method, and is meant to clarify some of the subtleties in the following Visual Basic .NET help article:
http://msdn2.microsoft.com/en-us/library/fs2xkftw(vs.71).aspxRefer to this Help document for detailed information about error handling and for general examples of the Dispose method.
A base class needs to contain an overloaded set of Dispose methods. The first instance of the sample code that follows is a version without parameters, and the second instance accepts a Boolean parameter:
Dispose() is the method that is called when an object is disposed of in the code in which the object was created. This is a Public method, and therefore it can be used when an instance of the class exists. The Dispose() method then calls the Dispose(Boolean) method and passes a value of TRUE. The Dispose(Boolean) method is responsible for cleaning up the resources of the class.
When a class is derived from a base class, only the Dispose(Boolean) method needs to be overridden. All resource-cleanup for the derived class will be performed in this overridden method, and then the Dispose(Boolean) method for the base class is called. The following is a primitive example of the function overriding the base class:
The derived class does not need a Dispose() method, because that method is inherited from the base class. When Dispose() is called on an instance of the derived class, Dispose() uses the Dispose(Boolean) of the derived class rather than the one in the base class. It is then important that the Dispose(Boolean) method of the derived class calls the Dispose(Boolean) method of the base class. This is done by means of the MyBase.Dispose(disposing) method. The Dispose(Boolean) method for the base class must be called to ensure that the resources of the base class are also disposed of.
Dispose() is meant as an entry point for public access to the disposal of an object and to ensure that TRUE is passed to the Dispose(Boolean) method. FALSE should be passed only when the Dispose(Boolean) method is called by the runtime or Finalize method. When FALSE is passed, only the unmanaged resources will be disposed. When TRUE is passed, both the managed and unmanaged resources are disposed.
The Visual Studio Development Environment inserts the code to override the Dispose() method into a class that inherits a system object (for example, Inherits System.Windows.Forms.TextBox). This is performed from the menus (at the top of the code window, by default) by selecting Overrides and then clicking Dispose(). The code that is inserted looks something like the following:
If this is done, no compile errors are raised. However, when the derived class is loaded at runtime, you receive a runtime error message similar to the following:
NOTE: The Type value will be different from that in the preceding example. That is merely the name of the class that attempted to use an improperly overridden Dispose() method.
An unhandled exception of type 'System.TypeLoadException' occurred in system.windows.forms.dll.
Additional information: Declaration referenced in a method implementation can not be a final method. Type: ClassLibrary1.UserControl1. Assembly: Dispose.
To correct this issue, just overload the Dispose(Boolean) method instead of Dispose(), and make sure that a call is made to the Dispose(Boolean) method of the base class and that TRUE is passed to it.
NOTE: In Visual Basic .NET or in Visual Basic 2005, the Overridable keyword is used like the Virtual keyword in C# and C++. Methods are, by default, NotOverridable.
Related Help Topics:
Contact us for more help
Connect with Answer Desk for expert help.