473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overload resolution failed

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

Jun 7 '06 #1
4 2676
"Kevin Burton" <Ke*********@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/>

Jun 7 '06 #2
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

"Kevin Burton" <Ke*********@discussions.microsoft.com> escribió en el
mensaje news:9E**********************************@microsof t.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

Jun 7 '06 #3
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.

Jun 7 '06 #4
> 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
Jun 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eph0nk | last post by:
Hi, I get an overload resolution failed error (Overload resolution failed because no accessible 'New' can be called without a narrowing conversion), and I can't seem to find a lot of relevant...
5
by: Gang Zhang | last post by:
Hi, I have 2 overloaded functions as below: myfunc(byval val as Decimal) as string myfunc(byval val as String) as string When calling the function with a single like: dim num1 as single...
0
by: Cailin | last post by:
I am trying to make a connection with my SQL server DB, and I got this error message: "BC30519: Overload resolution failed because no accessible 'New' can be called without a narrowing...
1
by: steve | last post by:
I am trying to create an XSLT Transform but keep getting the same problem. Overload resolution failed because no accessible 'Transform' can be called with these arguments. I create a reference...
0
by: bcobra | last post by:
What am I doing wrong? the code: age Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"...
2
by: jason | last post by:
Since going to framework 2.0 from 1.1, I'm getting error: Overload resolution failed because no accessible 'New' can be called without a narrowing conversion: On line: Dim LogInfo As New...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I have a strange situation with vb 2005 regarding option strict that looks like a compiler bug to me. Consider: Dim f1 As New Font("Arial", 8.25) Dim f2 As New Font("Arial", 8.25,...
2
by: Bart | last post by:
Hi, i get the error "BC30518:Overload resolution failed because no accessible 'Join' can be called with these arguments" at line: hvd = Join(hvertp, ",") Any idea what's wrong here? Thanks...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.