DescriptionThe Protected keyword is used to set the access level for classes and class members. By including this keyword, the associated class or member can be accessed by the entire class, and by all derived classes, but not by any other code. Declaring a class module as Protected limits all of the class's members to Protected access (or stronger, if the member has further specific access restrictions). When combined with the Friend keyword, the related element takes on all aspects of both the Protected and Friend keywords. The Protected keyword can be used with the following statements:
Even among these statements, the Protected keyword is only valid in the context of a class definition. ExampleConsider the following class declaration. Public Class MainClass Protected internalData As String End Class Then, within MainClass or any of its derived classes in any project, the internalData variable can be accessed directly: Public Class LaterClass Inherits MainClass Public Sub Test( ) MsgBox(internalData) End Sub End Class However, code that uses MainClass, but does not derive from it, cannot use the variable. Dim intruderAlert As New MainClass ' ----- The following line will not compile. intruderAlert.internalData = "Secret" Version DifferencesThe Protected keyword is new to VB under .NET. See AlsoFor the statements listed above, see the related entries elsewhere in this chapter for usage information. For information on using the Protected keyword as a statement, see the entry for the Dim Statement. Friend Keyword, Private Keyword, Public Keyword |