I have a class like this:
class Foo
private shared default_ = "DEFAULT"
public shared function bar(val as object) as string
if val is Nothing then return _default
return getBar(val)
end function
private shared function getBar(val as string) as string
return formatString(val)
end function
private shared function getBar(val as System.Int32) as string
return formatInt(val)
end function
end class
formatString and FormatInt being public shared methods. When I call Foo.bar, I get a MissingMemberException:
System.MissingMemberException: Public member 'getBar' on type 'Foo' not found.
It works, when I make getBar methods public, but I don't want to expose these unneccessarily. Why I can't call private shared methods from a public one in the same class is over my head.
I'm using .net framework 4.0 in a web application.
Your public method takes an object as parameter and your private methods are taking string/integer. So the runtime does not know which overloaded method it should use.
If you make all overloaded methods public
, the code only would compile if you have Option Strict Off. Then on runtime no exception migth be thrown because the actual type of the object will be detected via late-binding(or even silently casted to a matching type, f.e. from date
to string
). But nevertheless, avoid late binding with Option Strict On
and use VB.NET as what it is supposed to be: a strong typed language.
The Main-Reason why your code throws a MissingMemberException
with Option Strict Off
and private methods is:
You could check the type of val
, cast the object to this type and then pass that parameter to the correct method. For example:
Public Overloads Shared Function bar(ByVal val As Object) As String
If val Is Nothing Then Return default_
If TypeOf val Is String Then
Return getBar(CStr(val))
ElseIf TypeOf val Is Int32 Then
Return getBar(CInt(val))
Else
Return default_
End If
End Function
Another point: if you plan to create an Extension-Method that takes an object, forget it:
Mainly, we do not allow extension methods to be called off of any expression that is statically typed as "Object". This was necessary to prevent any > existing late bound code you may have written from being broken by extension methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With