Compiler Error With Protected Friend


N

Neal

Hi all

Why do I get a compiler error with the following code.

Friend Class SomeClass
End Class

Public Class SomePublicClass

Protected Friend Function AMethod As SomeClass
End Function
End Class

Both the Protected Method and the SomeClass have the Same Scope (Friend) so
there should be no problem.

Regards
Neal
 
Ad

Advertisements

1

1388-2/HB

I think this is what the compiler is trying to avoid:


Public Class SomePublicClass
Protected Friend Function AMethod As [Type]
End Function
End Class

Public Class NotAFriend
Inherits YourProject.SomePublicClass
Sub New()
Dim obj As Object = MyBase.AMethod 'ambiguous scope
End Sub
End Class
 
N

Neal

Yes I agree, but surely it should not even be considered as being ambiguous
since the AMethod should never be available outside the project in the first
place since it's scope is of Friend. It appears Protected methods simply
ignore the Friend scope. If that's the case then the VB Editor really should
be removing the Friend Scope or showing an error in Error List saying
Protected Methods can only be of Public scope.

Regards
Neal

1388-2/HB said:
I think this is what the compiler is trying to avoid:


Public Class SomePublicClass
Protected Friend Function AMethod As [Type]
End Function
End Class

Public Class NotAFriend
Inherits YourProject.SomePublicClass
Sub New()
Dim obj As Object = MyBase.AMethod 'ambiguous scope
End Sub
End Class



Neal said:
Hi all

Why do I get a compiler error with the following code.

Friend Class SomeClass
End Class

Public Class SomePublicClass

Protected Friend Function AMethod As SomeClass
End Function
End Class

Both the Protected Method and the SomeClass have the Same Scope (Friend)
so there should be no problem.

Regards
Neal
 
Ad

Advertisements

A

Armin Zingler

Neal said:
Yes I agree, but surely it should not even be considered as being
ambiguous since the AMethod should never be available outside the
project in the first place since it's scope is of Friend. It appears
Protected methods simply ignore the Friend scope. If that's the case
then the VB Editor really should be removing the Friend Scope or
showing an error in Error List saying Protected Methods can only be
of Public scope.

"Protected Friend" does /not/ mean that the method is only visible to
inherited classes inside the same assembly. Instead it means that the method
is accessible from
a) all classes inside the same assembly
/as well as/
b) from inherited classes outside the assembly.

Due to b), the type "SomeClass" must be public, too.

In other words,
- "Protected" means that it is accessible from all inherited classes, even
outside the assembly.
- "Friend" means that it is accessible only inside the assembly but not only
limited to the inherited classes.

So, "Protected Friend" is not more limiting than Protected or Friend alone,
it is less limiting.

(see also F1)


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top