Name

Protected Keyword

Description

Used to declare classes and their members.

When the Protected keyword is used to modify a member declaration, the member being declared has direct access scope to the class module in which the member is declared, as well as to all derived classes in all projects. However, as far as object access is concerned, the member is considered Private; that is, it can only be accessed within the declaring class. (See the upcoming example.)

Declaring a class module as Protected limits all of the class’ members to Protected access (or stronger if the member has further specific access restrictions).

Example

Suppose we declare the following variable in a class module named Class1:

Protected sProtectedVar As String

Then within Class1 or any of its derived classes in any project, we can use the variable directly, as in:

Public Class Class2
   Inherits Class1

   Public Sub Test(  )
      MsgBox sProtectedVar
   End Sub

End Class

On the other hand, the following code, located in a form module, is illegal:

Dim c as New Class1
c.sProtectedVar = "Donna"

VB.NET/VB 6 Differences

The Protected keyword is new to VB.NET.

See Also

Friend Keyword

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.