Microsoft KB Archive/818216

= BUG: &quot;Public Overrides WriteOnly Property...&quot; error when you try to override a Microsoft Visual Basic 6.0 property in Microsoft Visual Basic .NET =

Article ID: 818216

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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.



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



WORKAROUND
To work around this bug, pass the argument as ByVal instead of as ByRef in the base class. If you pass the argument as ByVal, actual data is copied instead of a pointer to the data.

The following code sample describes how to pass the argument as ByVal: Option Explicit

Private m_intNumber As Integer

'Previous Implementation 'Public Property Let myProp(ByRef intNumber As Integer) ' m_intNumber = intNumber 'End Property

'New Implementation Public Property Let myProp(ByVal intNumber As Integer) m_intNumber = intNumber End Property

Public Function ReturnNumber As Integer ReturnNumber = m_intNumber End Function



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Start Visual Basic 6.0. On the New Project dialog box, click the New tab. Click to select the ActiveX DLL project, and then click Open.

By default, Class1 is created.  Paste the following code in the Code window of Class1: Option Explicit

Private m_intNumber As Integer

Public Property Let myProp(ByRef intNumber As Integer) m_intNumber = intNumber End Property

Public Function ReturnNumber As Integer ReturnNumber = m_intNumber End Function  On the File menu, click Make Project1.dll. On the File menu, click Exit to close the Visual Basic 6.0 IDE.</li> Start Microsoft Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> Under Project types, click Visual Basic Projects.</li> Under Templates, click Class Library.

By default, Class1 is created.</li> In Solution Explorer, right-click References, and then click Add References.</li> On the COM tab, locate Project1.dll, click Select, and then click OK.</li>  Replace the existing code with the following code in Class1: Public Class Class1 Inherits Project1.Class1Class

Public Overrides WriteOnly Property myProp As IntPtr Set(ByVal Value As IntPtr)

End Set End Property End Class </li> On the Build menu, click Build Solution.

You receive the error mentioned in the &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">