Scope and Accessibility in Class Modules

The notion of scope in class modules is more involved than it is in standard modules. As far as local variables (block-level and procedure-level) are concerned, there is no difference—we have block scope and procedure-level scope.

However, variables declared in the Declarations section of a class module can be assigned one of the following access modifiers:

  • Public

  • Private

  • Friend

  • Protected

  • Protected Friend

(For standard modules, only Public, Private, and Friend are allowed.)

Note that class modules themselves can be declared with any one of the three access modifiers: Public, Private, or Friend (Protected is not allowed). When a class module declaration specifies one of these access modifiers, this simply restricts all of its members to that level of access, unless a member’s access is further restricted by the access modifier on the member declaration itself. For instance, if the class has Friend access, no member can have Public access. (Put another way, the Public access is overridden by the Friend class access.)

On the other hand, all four access modifiers apply to members of the class module—that is, to variable, constant, enum, and procedure declarations within the class module.

The complications come because there are actually three types of access to a class member, and these generally have different scopes. To clarify, let’s make the following definitions, which are not standard but descriptive. For example, consider a ...

Get VB .NET Language in a Nutshell 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.