Previous | Index | Next 

[PRB] The VB.NET application doesn’t compile because of a “Method XYZ not found” compilation error

Note: this article only applies to conversions to VB.NET.

If a VB6 class has a method with a Friend scope and the class is implemented as an interface, the following compilation error can appear after the conversion to VB.NET:

        Method 'XYZ' not found

For example, consider a VB6 class named Rectangle:

        ' (inside the Rectangle class)
        Friend Sub Init(ByVal width As Single, ByVal height As Single)
            ' store width/height in member variables
        End Sub
        ' ... more class members here...

Let’s further assume that the VB6 application includes a Square class defined as follows:

        ' (inside the Square class)
        Implements Rectangle
        ' ... class members here...

The Implements Rectangle statement forces VB Migration Partner to render Rectangle as an interface and to rename the original Rectangle class as RectangleClass:

        ' (VB.NET code)
        Public Class RectangleClass
            Implements Rectangle

            Friend Sub Init(width As Single, height As Single)
                ' store width/height in member variables
            End Sub
            ' ... add more class members here...
        End Class

Finally, assume that elsewhere in the VB6 project you have the following method:

        Function GetRectangle(ByVal width As Single, ByVal height As Single) As Rectangle
            Set GetRectangle = New Rectangle
            GetRectangle.Init width, height
        End Function

Here’s how VB Migration Partner translates the GetRectangle method to VB.NET:

        Function GetRectangle(ByVal width As Single, ByVal height As Single) As Rectangle
            GetRectangle = New RectangleClass()
            GetRectangle.Init(width, height)
        End Function

Here’s the problem: the GetRectangle local variable contains a pointer to the Rectangle interface, not to the RectangleClass concrete class. The Init method has a Friend scope and isn’t part of the Rectangle interface, therefore it can’t be reached through the reference in the GetRectangle variable. You can solve this problem by slightly tweaking the VB6 code, as follows:

        Function GetRectangle(ByVal width As Single, ByVal height As Single) As Rectangle
            Dim rect As New Rectangle
            rect.Init width, height
            Set GetRectangle = rect
        End Function

which translates to the following VB.NET code:

        Function GetRectangle(ByVal width As Single, ByVal height As Single) As Rectangle
            Dim rect As New RectangleClass()
            rect.Init(width, height)
            Return rect
        End Function

This code compiles and runs correctly.

 

Previous | Index | Next