Understanding Scope

The scope of an element in your code is all the code that can refer to it without qualifying its name (or making it available through an Imports statement). In other words, an element's scope is its accessibility in your code. As we write larger programs, scope will become more important, because we'll be dividing code into classes, modules, procedures, and so on. You can make the elements in those programming constructs private, which means they are tightly restricted in scope.

In VB .NET, where you declare an element determines its scope, and an element can have scope at one of the following levels:

Block scope—available only within the code block in which it is declared

Procedure scope—available only within the procedure in which it is declared

Module scope—available to all code within the module, class, or structure in which it is declared

Namespace scope—available to all code in the namespace

For example, if you declare a variable in a module outside of any procedure, it has module scope, as in this case, where I'm declaring and creating a LinkLabel control that has module scope:

When you want to declare module-level variables, you can place the declaration outside any procedure in the module. You also can select the module in the left-hand drop-down list box in the code designer and the (Declarations) item in the right-hand drop-down box, which will take you to a location at the very beginning of the module, outside any procedure.

Declaring a variable in a procedure gives it procedure scope, and so on. Inside these levels of scope, you can also specify the scope of an element when you declare it. Here are the possibilities in VB .NET, which we'll become familiar with throughout the book:

Public —The Public statement declares elements to be accessible from anywhere within the same project, from other projects that reference the project, and from an assembly built from the project. You can use Public only at module, namespace, or file level. This means you can declare a Public element in a source file or inside a module, class, or structure, but not within a procedure.

Protected —The Protected statement declares elements to be accessible only from within the same class, or from a class derived from this class. You can use Protected only at class level, and only when declaring a member of a class.

Friend —The Friend statement declares elements to be accessible from within the same project, but not from outside the project. You can use Friend only at module, namespace, or file level. This means you can declare a Friend element in a source file or inside a module, class, or structure, but not within a procedure.

Protected Friend —The Protected statement with the Friend keyword declares elements to be accessible either from derived classes or from within the same project, or both. You can use Protected Friend only at class level, and only when declaring a member of a class.

Private —The Private statement declares elements to be accessible only from within the same module, class, or structure. You can use Private only at module, namespace, or file level. This means you can declare a Private element in a source file or inside a module, class, or structure, but not within a procedure.

Let's take a look at an example. Here's what block scope looks like—in this case, I'll declare a variable, strText in an If statement. That variable can be used inside the If statement's block, but not outside (VB .NET will tag the second use here as a syntax error):

Here's another example. In this case, I've created a second module, Module2, and defined a function, Function1, in that module. To make it clear that I want to be able to access Function1 outside Module2 (as when I call it as Module2.Function1 in the Main procedure), I declare Function1 public:

Note that in this case, I've put Module2 into the same file as Module1. You can also create a new file for Module2 if you prefer—just select Project|Add New Item and then select Module in the Templates box of the Add New Item dialog that opens.

However, if I declared Function1 as private to Module2, it's inaccessible in Module1 (and VB .NET will tag Module2.Function1 below as a syntax error):

Besides procedures, you also can make other elements—such as variables—public or private. Here, I'm declaring strData as public in Module2 to make it clear that I want to access it outside the module, which I can do in Module1, referring to strData as Module2.strData:

Now that VB .NET is object-oriented, understanding scope is more important. In object-oriented programming, scope becomes a major issue, because when you create objects, you often want keep the data and code in those objects private from the rest of the program. Scope also becomes an issue when you derive one OOP class from another—we'll see a great deal more on this issue in Chapters 11 and 12.