Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call private shared methods from a public shared method in vb.net?

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.

like image 451
gpinkas Avatar asked Aug 25 '11 09:08

gpinkas


1 Answers

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:

  • If you are attempting to access members on a late-bound object variable, make sure it is declared Public(Protected , Friend, and Private variables cannot be late-bound in Visual Basic)

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

  • http://msdn.microsoft.com/en-us/library/bb384936.aspx (look for MissingMemberException)
like image 184
Tim Schmelter Avatar answered Nov 03 '22 21:11

Tim Schmelter