Overload resolution failed

G

Guest

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
 
H

Herfried K. Wagner [MVP]

Kevin Burton said:
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.
 
R

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
 
A

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

Jim Wooley

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top