尽管在内部执行了调用,但未找到类型为“ <模块>”的公共成员“ <方法>”

时间:2019-12-13 15:34:59

标签: .net vb.net visual-studio-2010 polymorphism

我编写了一个类似于以下示例的模块:

Public Module aModule

  Private Sub doSomethingRelevant()
    Dim s As String = "foo"
    Dim o As Object = s
    aModule.performCrashyThing(s) ' Success
    aModule.performCrashyThing(o) ' Error
  End Sub

  Private Sub  performCrashyThing(s as String)
    ' Blah blah
  End Sub

  Private Sub  performCrashyThing(l as Long)
    ' Blah blah
  End Sub

End Module

第二个调用performCrashyThing(o)引发异常:

  

未找到类型为“ aModule”的公共成员“ performCrashyThing”。

如果我打开此子例程Public(不用使用Long参数触摸第二个子例程),它不会发牢骚。我不明白,因为调用是在模块内部执行的。

2 个答案:

答案 0 :(得分:0)

正如我在上面的评论中所述,“严格启用”位于: (但您需要始终打开并更改方法)enter image description here

答案 1 :(得分:0)

不确定要完成传递对象的目的。这里有一些值得深思的地方。我添加了performCrashyThing的对象方法,该方法确定应调用哪个特定的performCrashyThing。

Public Module aModule

    Public Sub doSomethingRelevant()
        Dim s As String = "foo"
        Dim o As Object = s
        aModule.performCrashyThing(s)
        aModule.performCrashyThing(o)
        o = 1L
        aModule.performCrashyThing(o)
    End Sub

    Private Sub performCrashyThing(obj As Object)
        If TypeOf obj Is String Then
            performCrashyThing(DirectCast(obj, String))
        ElseIf TypeOf obj Is Long Then
            performCrashyThing(DirectCast(obj, Long))
        Else
            Stop
        End If
    End Sub

    Private Sub performCrashyThing(s As String)
        ' Blah blah
    End Sub

    Private Sub performCrashyThing(l As Long)
        ' Blah blah
    End Sub

End Module