Access Modifiers

An access modifier determines which class methods—including methods of other classes—can see and use a member variable or method within a class. Table 18-1 summarizes the Visual Basic 2005 access modifiers .

Table 18-1. Access modifiers

Access modifier

Restrictions

Public

No restrictions. Members marked Public are visible to any method of any class.

Private

The members in class A which are marked Private are accessible only to methods of class A.

Protected

The members in class A which are marked Protected are accessible to methods of class A and also to methods of classes derived from class A.

Friend

The members in class A which are marked Friend are accessible to methods of any class in A's assembly.

Protected Friend

The members in class A which are marked Protected Friend are accessible to methods of class A, to methods of classes derived from class A, and also to any class in A's assembly. This is effectively Protected OR Friend (There is no concept of Protected AND Friend.)

Tip

The Protected access modifier is used with derived classes, as explained below in the discussion on inheritance. The Friend accessor says that the members are visible to any method within the Assembly. An assembly is a collection of files that appear to the programmer as a single executable (.exe) or DLL.

It is good programming practice to explicitly set the accessibility of all methods and members of your class.

Get Programming Visual Basic 2005 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.