戻り値の型にアクセスできないため、このコンテキストでは '<メソッド名>' にアクセスできません

呼び出し元のステートメントからアクセスできない戻り値の型を持つ関数を呼び出しました。 たとえば、次のコードでは、 Main から PublicMethod への呼び出しは戻り値の型 PrivateTypeが、クラス PrivateTestClassアクセス修飾子で宣言されているために失敗します。 そのため、 PrivateType にアクセスできるコンテキストは、 TestClassに限定されます。

Class TestClass

    Dim pT As New PrivateType

    Private Class PrivateType
    End Class

    '' A corresponding error is returned in this line: 'PublicMethod
    '' cannot expose 'PrivateType' in namespace 'ConsoleApplication1'
    '' through class 'TestClass'.
    'Public Function PublicMethod() As PrivateType
    '    Return Nothing
    'End Function

End Class

Module Module1

    Sub Main()

        Dim tc As TestClass
        '' This error occurs here, because the data type returned by
        '' PublicMethod()is declared Private in class TestClass and
        '' cannot be accessed from here.
        'Console.WriteLine(tc.PublicMethod())

    End Sub

End Module

エラー ID: BC36665 および BC36666

このエラーを解決するには

  • 型定義がアクセス可能な場合、アクセス修飾子を Private から Publicに変更します。

  • アクセス権がある場合は、関数の戻り値の型を変更します。

  • アクセス可能な型を返すメソッドまたは拡張メソッドを記述します。

関連項目