Protected Keyword


Protected Keyword

Description

The 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:

Class Statement
Const Statement (but not for local constants)
Declare Statement
Delegate Statement
Dim Statement (but not for local variables)
Enum Statement
Event Statement
Function Statement
Interface Statement
Property Statement
Structure Statement
Sub Statement

Even among these statements, the Protected keyword is only valid in the context of a class definition.

Example

Consider 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 Differences

The Protected keyword is new to VB under .NET.

See Also

For 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




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net