1

I am currently working on a VB.Net project and came across something that befuddled me a little bit. So I essentially have the following code structure:

Public Class MainClass

    Private Class HiddenClass
    End Class

    Private Class ChildA
        Inherits From HiddenClass
    End Class

    Private Class ChildB
        Inherits From HiddenClass
    End Class

End Class

Public Class BuildingBlock

    Protected lbl As Label
    Protected btn As Button
    Protected main As New MainClass()

End Class

Now I am getting an error from setting main as Protected. Specifically the error I get is:

'main' cannot expose type 'MainClass' outside the project through class 'BuildingBlock'

Now I Googled the error, and found the solution was to:

Change the access level of the variable, procedure parameter, or function return to be at least as restrictive as the access level of its data type.

So, I changed it to Public, and everything was perfectly fine. But just to test thing, I changed to access to Private, Friend, and Protected Friend. Protected Friend still had the error as I was expecting, but both Private and Friend did not, even though they are different access levels than that of the data type.

So I am wondering why I would only get this error for listing this object as Protected, and not at any other level of access.

Skitzafreak
  • 1,797
  • 7
  • 32
  • 51
  • 1
    The posted code does not produce that error. – LarsTech May 30 '18 at 20:33
  • 1
    He meant Friend instead of Public. Friend is the default if no accessibility keyword is used. Which permits only code that is inside the same project to use MainClass. Written by friends. The problem is that somebody could derive a class from BuildingBlock in another project but won't be able to access the main member at all. Which is bad of course. The CLR actually allows this kind of accessibility, C# just recently took advantage of it with *private protected* in version 7.2. Afaik vb.net did as well, you'd have to [hack the langversion](https://stackoverflow.com/a/32123363/17034). – Hans Passant May 30 '18 at 22:40

0 Answers0