Scope and Accessibility in Class Modules VB.NET

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 variable declaration in the Declaration section of a class module named Class1:

AccessModifier classvariable As Integer

This variable can be accessed in the following ways:

Direct access

Refers to accessing the member without any qualification, as in:

classvariable = 100

When attempting to access a variable using direct access (that is, without qualification), the variable's scope takes one of three forms:

  • The declaring class only
  • The declaring class and its derived classes within the declaring project only
  • The declaring class and its derived classes, in any project that holds a reference to the declaring project

Class/object access

Refers to accessing the member through qualification, either with the class name or the name of an object of that class.

As we have discussed, if a member variable is declared using the Shared keyword, then it is shared by all objects in the class. More accurately, the member exists independently of any object of the class. In this case, the member can be accessed (within its scope) through qualification by the class name, as in:

Class1.classvariable = 100

Note that the member can also be accessed through qualification by an object name, but this has the same effect as access through qualification by the class name—there is only one copy of the member.

If the member is declared without using the Shared keyword, then class/object access refers to accessibility through qualification by the name of an existing object, as in:

The scope for class/object access can be one of the following:

  • The declaring class only
  • The declaring project
  • The declaring project and any external software component that holds a reference to the declaring project

Table describes the effects of the various access modifiers.

Access modifiers in class modules

Access modifiers in class modules

Unfortunately, it does not seem possible to make a simple statement about the effect of the access modifiers Friend and Protected independently. It would have been much clearer to have separate sets of access modifiers for direct-access scope and class/object scope, instead of intertwining the concepts as shown.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

VB.NET Topics