P
Pat Kelly via .NET 247
I have the following code that will not compile. It gives'A.Protected Sub SomeMethod()' is not accessible in this contextbecause it is 'Protected'. Class B can call the inherited method(SomeMethod) on itself just fine but not on a different instanceof Class A or Class B.
Public Class A
Protected Sub SomeMethod()
End Sub
Protected Sub SomeOtherMethod(ByVal objA As A)
objA.SomeMethod() 'No compile error - instance of A can callmethod on other instance
End Sub
End Class
Public Class B
Inherits A
Protected Sub AnotherMethod(ByVal objA As A)
objA.SomeMethod() 'Compile error
End Sub
Protected Sub YetAnotherMethod(ByVal objB As B)
objB.SomeMethod() 'No Compile error
End Sub
End Class
MSDN says protected methods are only accessible from within theirown class or a derived class, but do not mention anything aboutbeing accessible only to the current instance. I am also puzzledby the inconsistency between the way the base class and thederived class handle calls to another instance. A can call aprotected method on another instance of A, B can call theinherited method on another instance of B, but B cannot call theinherited method on an instance of A. Can anyone explain this?
Public Class A
Protected Sub SomeMethod()
End Sub
Protected Sub SomeOtherMethod(ByVal objA As A)
objA.SomeMethod() 'No compile error - instance of A can callmethod on other instance
End Sub
End Class
Public Class B
Inherits A
Protected Sub AnotherMethod(ByVal objA As A)
objA.SomeMethod() 'Compile error
End Sub
Protected Sub YetAnotherMethod(ByVal objB As B)
objB.SomeMethod() 'No Compile error
End Sub
End Class
MSDN says protected methods are only accessible from within theirown class or a derived class, but do not mention anything aboutbeing accessible only to the current instance. I am also puzzledby the inconsistency between the way the base class and thederived class handle calls to another instance. A can call aprotected method on another instance of A, B can call theinherited method on another instance of B, but B cannot call theinherited method on an instance of A. Can anyone explain this?