Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Method overload requires Public declaration
Message
De
07/05/2011 02:11:44
 
 
À
06/05/2011 17:04:54
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01509881
Message ID:
01509929
Vues:
51
>>>In the same class, I created a private method. Then, I overloaded the method so it could also accept a different type for the parameter. So, the first declaration accepts an integer. The overaload declaration accepts a string. When running the application, I got an error telling me that the method was not found. I had to make them Public so to have it work. Why is that?
>>
>>From your description:
Class SomeClass
>>	Private Sub Test()
>>		DoSomething("Hello")
>>		DoSomething(3)
>>	End Sub
>>
>>	Private Sub DoSomething(s As String)
>>	End Sub
>>	Private Sub DoSomething(i As Integer)
>>	End Sub
>>End Class
>>
What doesn'r work ?
>
>I got confused from this message:
>
>Overload resolution failed because no Public 'GetPhone' can be called without a narrowing conversion:
> 'Public Function GetPhone(tcTelephone As String) As String':
> Argument matching parameter 'tcTelephone' narrows from 'Decimal' to 'String'.
> 'Public Function GetPhone(tnTelephone As Integer) As String':
> Argument matching parameter 'tnTelephone' narrows from 'Decimal' to 'Integer'.

That sounds like a gotcha in the VB implicit conversion rules with Option Strict OFF. If you have two overloads (one accepting a string and the other an integer) and attempt to call that function with a decimal value as the parameter then the compiler doesn't know whether it should implicitly convert to a string or an integer (ie which overload to use). You should cast to the type you wish to use prior to the call.
(In C# the same construct would give the usual 'invalid argument' error).
But I'm not sure why you saw this only at runtime - the compiler should catch it?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform