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:
(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:
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:
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:
Table describes the effects of the various access modifiers.
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.
VB.NET Related Interview Questions
|VB.NET Interview Questions||ASP.NET Interview Questions|
|ADO.Net Interview Questions||Windows Presentation Foundation(WPF) Interview Questions|
|Windows CE .NET Interview Questions||Dot Net Framework Interview Questions|
|Asp Dot Net Mvc 4 Interview Questions||Asp Dot Net Mvc Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.