Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘class’

BUG: “Public Overrides WriteOnly Property…” error when you try to override a Microsoft Visual Basic 6.0 property in Microsoft Visual Basic .NET

Symptoms
You have a Microsoft Visual Basic 6.0 class that has a property with the ByRef parameter. When you inherit the class in Visual Basic .NET and override the property, you receive the following compilation error:

‘Public Overrides WriteOnly Property myProp() As System.IntPtr’ cannot override ‘Public Overridable Overloads WriteOnly Property myProp() As System.IntPtr’ because they differ by their return types.
Resolution
The .NET runtime compares the return types of the parameters in the base class and inherited class property. This comparison returns a difference in the return types while it compares the symbols for System.IntPtr and System.Int16. Two separate symbols represent these internally. This results in an error.

How to implement the Dispose method in a derived class in Visual Basic .NET or in Visual Basic 2005

Symptoms
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).aspx(http://msdn2.microsoft.com/en-us/library/fs2xkftw(vs.71).aspx) Refer to this Help document for detailed information about error handling and for general examples of the Dispose method.
Resolution
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:

‘Method that is called by Public to ensure TRUE is passed to DisposePublic Overloads Notoverridable Sub Dispose()Dispose( TRUE )’ Take yourself off of the finalization queue.GC.SuppressFinalize(Me)End Sub’Method that does the actual disposal of resourcesProtected Overloads Overridable Sub Dispose(ByVal disposing As Boolean)’Clean Up ResourcesEnd Sub
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:

Protected Overloads Overrides Sub Dispose(disposing As Boolean)’Clean Up ResourcesMyBase.Dispose( disposing )End Sub 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:

Public Overloads Overrides Sub Dispose()’Clean Up ResourcesEnd Sub 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:

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.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.
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.

BUG: IDE Crash with Compiled GlobalMultiUse

Symptoms
If the Terminate event of a GlobalMultiUse class calls a procedure inanother DLL, and if the same GlobalMultiUse class had previously called thesame DLL, then you receive an error and the IDE may crash.
With Visual Basic 5.0, you may receive one of the following errors:

Exception: privileged instruction (0xc0000096), Address: 0×00186a3a
-or-

VB5 caused an invalid page fault (or general page fault)
With Visual Basic 6.0, the IDE does not crash, but you may receive thefollowing error:

Run-time error ‘-2147418105 (80010007)’:
Automation error
This error translates to:

“The callee (server [not server application]) is not available and
disappeared; all connections are invalid. The call may have executed.”
Resolution
This problem can occur when the ActiveX components are not shut down in thenecessary order when your client application ends. For example, supposeyour client application uses two ActiveX DLLs (DLL1 and DLL2). If theTerminate event of a class in DLL2 calls a procedure in DLL1, but DLL1 hasbeen shut down before DLL2, you can experience this problem because theprocedure in DLL1 is unavailable. The “Steps to Reproduce Behavior” sectionof this article demonstrates this scenario and shows how to work around theproblem by controlling the order in which the two DLLs shut down.

BUG: “System.MissingMethodException” error message when you try to build an application that hosts a licensed class

Symptoms
If you try to build an application that hosts a licensed class, and the application does not have a default (parameter-less) constructor, you receive an error message that is similar to the following:

Could not transform licenses file ‘licenses.licx’ into a binary resource. (1) : error LC0004 : Exception occured creating type ‘System.MissingMethodException’ Note In this error message, the word “occured” is a misspelling of the word “occurred.”
Resolution
To resolve this problem, add a default (parameter-less) constructor to the licensed class and then rebuild the licensed class library.