BUG: "Public Overrides WriteOnly Property..." error when you try to override a Microsoft Visual Basic 6.0 property in Microsoft Visual Basic .NET (818216)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Basic 6.0.
  2. On the New Project dialog box, click the New tab.
  3. Click to select the ActiveX DLL project, and then click Open.

    By default, Class1 is created.
  4. 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
    
  5. On the File menu, click Make Project1.dll.
  6. On the File menu, click Exit to close the Visual Basic 6.0 IDE.
  7. Start Microsoft Visual Studio .NET.
  8. On the File menu, point to New, and then click Project.
  9. Under Project types, click Visual Basic Projects.
  10. Under Templates, click Class Library.

    By default, Class1 is created.
  11. In Solution Explorer, right-click References, and then click Add References.
  12. On the COM tab, locate Project1.dll, click Select, and then click OK.
  13. 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
    
  14. On the Build menu, click Build Solution.

    You receive the error mentioned in the "Symptoms" section of this article.

REFERENCES

For more information about overriding properties and methods, visit the following MSDN Web site:

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbProperties kbMarshal kbinterop kbCodeGen kberrmsg kbWndwClass kbprb kbbug KB818216 kbAudDeveloper