开发者

"Public \ Friend" vs "Friend \ Friend"

开发者 https://www.devze.com 2023-02-28 12:33 出处:网络
I\'ve been reading about access modifiers in VB.Net lately, and there is something that I can\'t really unders开发者_高级运维tand: How do elements in a Class (or Module) inherit the modifiers of their

I've been reading about access modifiers in VB.Net lately, and there is something that I can't really unders开发者_高级运维tand: How do elements in a Class (or Module) inherit the modifiers of their enclosing block?

For example, suppose you have a Friend class Bla in an assembly, with a public method Foo:

Friend Class Bla
    Public Sub Foo
    (...)
End Class

Does it behave differently than when Foo is set to Friend? If so, which one do you advise?

Friend Class Bla
    Friend Sub Foo
    (...)
End Class

Thanks!


In my opinion, it doesn't matter whether one specified public or Friend for Foo because the enclosing class is available only in this assembly.

One can choose to restrict the access modifier for a method/property than that of the class. In your example, the method Foo can be private` as well, which means the method won't be available to callers, including other classes in the same assembly.

One can access methods through class/instance. If the class is private, what use is a public method?

EDIT: On a side note, it is possible for you to return an instance of Bla to the caller (which is in other assembly). In that case, the caller should be able to call public method, if Foo is declared public. This is my assumption.


If someone does jump through the reflection hoops required to access Bla, they don't need to do so again to then call Public Sub Foo, but they do for Friend Sub Foo.

0

精彩评论

暂无评论...
验证码 换一张
取 消