O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required