overload resolution failed

This is from Wrox Professional VB 2005. I checked with the Wrox site and there is no corrections available for the book.

This is a section on using reflection, inheritance and multiple interfaces. It seems most of the bases are covered, because up until these last errors the builds were successful.

overload resolution failed because no accessible 'ShowDistance' is most specific for these arguments:

ShowDistance(obj as Parent)not most specific
ShowDistance(obj as Interfaces.Ishared)not most specific

The actual code is this in form1

Private Sub btnInheritance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInheritance.Click
ShowDistance(New Poly)
ShowDistance(New Encapsulation)
End Sub

And ShowDistance is also a private sub in form1

Private Sub ShowDistance(ByVal obj As Parent)
MsgBox(obj.DistanceTo(10, 10))
End Sub

Here is the Parent class


Public MustInherit Class Parent
Public MustOverride Function DistanceTo(ByVal x As Single, _
ByVal y As Single) As Single
End Class

The other classes inherit Parent


Just to give examples, there are two external assemblies that are properly referenced.

Public Interface IShared
Function CalculateDistance(ByVal x As Single, ByVal y As Single) As Single
End Interface

Can someone help

dennist685




Answer this question

overload resolution failed

  • Mats Hagberg

    Assuming you do have two methods ShowDistance (I could only see one in your code, but the error message states there are two), one that takes the IShared interface, and another that takes a parent class, then the error is expected - consider the following simplified code:

    Module Module1
    
      Sub Main()
        test(New c1)
      End Sub
    
      Interface i1
      End Interface
    
      Class c0
      End Class
    
      Class c1
        Inherits c0
        Implements i1
      End Class
    
      Sub test(ByVal arg As c0)
      End Sub
    
      Sub test(ByVal arg As i1)
      End Sub
    
    End Module
    
    In sub main, the compiler has two options to call - the thing is,
    the argument we're giving it can be converted to any of them, so
    the compiler can't make a decision - you can desambiguate the call
    by casting the argument to the precise type the method you want to
    call takes: test(ctype(New c1,c0)) or test(ctype(New c1,i1)).


  • voip

    Both lines show similar error messages.

    The first line:Private sub showdistance(object as parent): not most specific
    The second line: Private sub showdistance(object as interfaces.ishared):not most specific.

    Here are the rest of the classes:
    As you can see the authors are probably cramming too many concepts into one app, a sure recipe for making an error somewhere.

    dennist685

    Main App:

    Imports interfaces
    Public Class Poly
    Inherits Parent
    Implements IShared

    Public Overrides Function DistanceTo(ByVal x As Single, ByVal y As Single) As Single Implements IShared.CalculateDistance
    Return x + y
    End Function


    End Class

    Imports interfaces
    Public Class Encapsulation
    Inherits Parent
    Implements IShared
    Private mX As Single
    Private mY As Single
    Public Overrides Function DistanceTo(ByVal x As Single, ByVal y As Single) As Single Implements IShared.CalculateDistance
    Return Abs(x - mX) + Abs(y - mY)
    End Function
    Public Property CurrentX() As Single
    Get
    Return mX
    End Get
    Set(ByVal value As Single)
    mX = value
    End Set
    End Property
    Public Property CurrentY() As Single
    Get
    Return mY
    End Get
    Set(ByVal value As Single)
    mY = value
    End Set
    End Property

    End Class

    Public MustInherit Class Parent
    Public MustOverride Function DistanceTo(ByVal x As Single, _
    ByVal y As Single) As Single
    End Class

    Public MustInherit Class Contact
    Private mID As Guid = Guid.NewGuid
    Private mName As String

    Public Property ID() As Guid
    Get
    Return mID
    End Get
    Set(ByVal value As Guid)
    mID = value
    End Set
    End Property

    Public Property Name() As String
    Get
    Return mName
    End Get
    Set(ByVal value As String)
    mName = value
    End Set
    End Property

    End Class

    Public Class Customer
    Inherits Contact
    Private mPhone As String
    Public Property Phone() As String
    Get
    Return mPhone
    End Get
    Set(ByVal value As String)
    mPhone = value
    End Set
    End Property
    End Class

    Interfaces assembly(class library)

    Imports System.Drawing
    Public Interface IShared
    Function CalculateDistance(ByVal x As Single, ByVal y As Single) As Single
    End Interface

    Public Interface IPrintableObject
    Sub Print()
    Sub PrintPreview()
    Sub RenderPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
    End Interface

    Objects (class library)

    Imports Interfaces
    Public Class External
    Implements IShared
    Public Function DistanceTo(ByVal x As Single, ByVal y As Single) _
    As Single Implements IShared.CalculateDistance
    Return x * y
    End Function
    End Class







  • Rafer_Smith

    You have these two calls:

    ShowDistance(New Poly)
    ShowDistance(New Encapsulation)

    Can you show us the Poly type and the Encapsulation type Also, knowing which line is giving the error will also help.

    Finally according to the error message, you have another ShowDistance method definition other than the one you showed us.



  • ARTsev

    Without seeing all classes it's hard to figure out, but I have a feeling that the Parent class should implement the IShared interface...

  • Larry Mason

    Thanks, Alex.

    This isn't the first error in the Wrox book.

    dennist685

  • overload resolution failed