Home All Groups Group Topic Archive Search About

Overload resolution failed

Author
7 Jun 2006 2:56 PM
Kevin Burton
I have a base (MustInherit/abstact) class that looks like:

Public MustInherit Class BaseClass
End Class

Then I have three derived classes:

Public Class DerivedA
  Inherits BaseClass
End Class

Public Class DerivedB
  Inherits BaseClass
End Class

Public Class DerivedC
  Inherits BaseClass
End Class

Since each of these classes are handled differently I have three Shared
Overloaded Sub's

Public Shared Sub BuildMessage(ByVal Product As DerivedA)
End Sub

Public Shared Sub BuildMessage(ByVal Product As DerivedB)
End Sub

Public Shared Sub BuildMessage(ByVal Product As DerivedC)
End Sub

When I compile I get:

Overload resolution failed because no accessible 'BuildMessage' can be
called with these arguments:

    'Public Shared Sub BuildMessage(Product As DerivedA)':
Option Strict On disallows implicit conversions from 'BaseClass' to
'DerivedA'.
    'Public Shared Sub BuildMessage(Product As DerivedB)':
Option Strict On disallows implicit conversions from 'BaseClass' to
'DerivedB'.
    'Public Shared Sub BuildMessage(Product As DerivedC)':
Option Strict On disallows implicit conversions from 'BaseClass' to
'DerivedC'.

This type of thing is done all the time with C# and C++. Since I am
relatively new to VB I am not sure how to get around this error. I would
rather not globally turn 'Option Strict' off, but if that is the only way
then I guess I must. Suggestions?

Thank you.

Kevin

Author
7 Jun 2006 3:03 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Kevin Burton" <KevinBur***@discussions.microsoft.com> schrieb:
>I have a base (MustInherit/abstact) class that looks like:
>
> Public MustInherit Class BaseClass
> End Class
>
> Then I have three derived classes:
>
> Public Class DerivedA
>  Inherits BaseClass
> End Class
>
> Public Class DerivedB
>  Inherits BaseClass
> End Class
>
> Public Class DerivedC
>  Inherits BaseClass
> End Class
>
> Since each of these classes are handled differently I have three Shared
> Overloaded Sub's
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedA)
> End Sub
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedB)
> End Sub
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedC)
> End Sub
>
> When I compile I get:
>
> Overload resolution failed because no accessible 'BuildMessage' can be
> called with these arguments:
>
>    'Public Shared Sub BuildMessage(Product As DerivedA)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedA'.
>    'Public Shared Sub BuildMessage(Product As DerivedB)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedB'.
>    'Public Shared Sub BuildMessage(Product As DerivedC)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedC'.

I think you forgot to post the method call:

\\\
Dim c As BaseClass = ...
BuildMessage(c)
///

This will raise the compile-time error you stated above because the type of
the object referenced by 'c' isn't known at compile-time and maybe there is
another derived type which is neither 'DerivedA' nor 'DerivedB' nor
'DerivedC' and thus the call would fail at runtime.

To solve the problem, cast 'c' to the type of the object or use the derived
type instead of 'BaseClass' for the variable.  Alternatively you could
remove the overloaded methods and type the parameter 'Product' as
'BaseClass', check the type of the object passed into the method and throw
an 'ArgumentException' if it is none of the predefined derived types.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Jun 2006 3:11 PM
RMT
I can't quite understand whats going on here - is BuildMessage a member of
the base class?  If so, declare it overridable and then override it once in
each derived class.

Public MustInherit Class BaseClass

    Public MustOverride Sub BuildMessage ()

End Class

Public Class DerivedA
    Inherits BaseClass

    Public Overrides Sub BuildMessage ()
            ...
    End Sub

End Class

Public Class DerivedB
    Inherits BaseClass

    Public Overrides Sub BuildMessage ()
            ...
    End Sub

End Class

Public Class DerivedC
    Inherits BaseClass

    Public Overrides Sub BuildMessage ()
            ...
    End Sub

End Class

Show quoteHide quote
"Kevin Burton" <KevinBur***@discussions.microsoft.com> escribió en el
mensaje news:9E928987-F3E0-4799-BDE1-47E5D43DF4CB@microsoft.com...
>I have a base (MustInherit/abstact) class that looks like:
>
> Public MustInherit Class BaseClass
> End Class
>
> Then I have three derived classes:
>
> Public Class DerivedA
>  Inherits BaseClass
> End Class
>
> Public Class DerivedB
>  Inherits BaseClass
> End Class
>
> Public Class DerivedC
>  Inherits BaseClass
> End Class
>
> Since each of these classes are handled differently I have three Shared
> Overloaded Sub's
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedA)
> End Sub
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedB)
> End Sub
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedC)
> End Sub
>
> When I compile I get:
>
> Overload resolution failed because no accessible 'BuildMessage' can be
> called with these arguments:
>
>    'Public Shared Sub BuildMessage(Product As DerivedA)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedA'.
>    'Public Shared Sub BuildMessage(Product As DerivedB)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedB'.
>    'Public Shared Sub BuildMessage(Product As DerivedC)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedC'.
>
> This type of thing is done all the time with C# and C++. Since I am
> relatively new to VB I am not sure how to get around this error. I would
> rather not globally turn 'Option Strict' off, but if that is the only way
> then I guess I must. Suggestions?
>
> Thank you.
>
> Kevin
>
Author
7 Jun 2006 3:16 PM
AlanT
> This type of thing is done all the time with C# and C++. Since I am
> relatively new to VB I am not sure how to get around this error. I would
> rather not globally turn 'Option Strict' off, but if that is the only way
> then I guess I must. Suggestions?
>

In general, Option Strict On, gives VB.Net 'C# equivalent' behaviour
when it comes to casting.  If the idiom works in C#/C++ then it should
be achievable in VB.Net with strict On.

What exactly are you doing when you get the overload error?

It seems that you might be attempting something like

    dim p as BaseClass = new DerivedA;

    BuildMessage(p)    ' expecting to use the DerivedA overload of
BuildMessage

I haven't been able to get that to work in either VB.Net or C# without
an explicit cast.

Is this what you attempting?


Alan.
Author
7 Jun 2006 3:32 PM
Jim Wooley
Show quote Hide quote
> I have a base (MustInherit/abstact) class that looks like:
>
> Public MustInherit Class BaseClass
> End Class
> Then I have three derived classes:
>
> Public Class DerivedA
> Inherits BaseClass
> End Class
> Public Class DerivedB
> Inherits BaseClass
> End Class
> Public Class DerivedC
> Inherits BaseClass
> End Class
> Since each of these classes are handled differently I have three
> Shared Overloaded Sub's
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedA) End Sub
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedB) End Sub
>
> Public Shared Sub BuildMessage(ByVal Product As DerivedC) End Sub
>
> When I compile I get:
>
> Overload resolution failed because no accessible 'BuildMessage' can be
> called with these arguments:
>
> 'Public Shared Sub BuildMessage(Product As DerivedA)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedA'.
> 'Public Shared Sub BuildMessage(Product As DerivedB)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedB'.
> 'Public Shared Sub BuildMessage(Product As DerivedC)':
> Option Strict On disallows implicit conversions from 'BaseClass' to
> 'DerivedC'.
> This type of thing is done all the time with C# and C++. Since I am
> relatively new to VB I am not sure how to get around this error. I
> would rather not globally turn 'Option Strict' off, but if that is the
> only way then I guess I must. Suggestions?

Since you want the method to be shared, you can't declare it in the base
class as MustInherit or Overridable. Thus, you are likely putting each BuildMessage
method inside the respective derived classes. If so, you may want to alter
their signature to:

  Public Class DerivedA
   Inherits BaseClass
   Public Shared Sub BuildMessage(ByVal Product as BaseClass)
      If TypeOf Product Is DerivedA Then
           'Process Product
      Else
           Throw New InvalidArgumentException
      End If
   End Sub
  End Class

Then in your calling code you can do the following:

   Dim Product as new DerivedA
   DerivedA.BuildMessage(Product)

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx