Scope in VB.net
By: Steven Holzner
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:
Dim LinkLabel1 As LinkLabel Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click LinkLabel1 = New LinkLabel() LinkLabel1.AutoSize = True LinkLabel1.Location = New Point(15, 15) ⋮
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:
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):
Module Module1 Sub Main() Dim intValue As Integer = 1 If intValue = 1 Then Dim strText As String = "No worries." System.Console.WriteLine(strText) End If System.Console.WriteLine(strText) 'Will not work! End Sub End Module
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:
Module Module1 Sub Main() System.Console.WriteLine(Module2.Function1()) End Sub End Module Module Module2 Public Function Function1() As String 'OK Return "Hello from Visual Basic" End Function End Module
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):
Module Module1 Sub Main() System.Console.WriteLine(Module2.Function1()) 'Will not work! End Sub End Module Module Module2 Private Function Function1() As String Return "Hello from Visual Basic" End Function End Module
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:
Module Module1 Sub Main() System.Console.WriteLine(Module2.strData) End Sub End Module Module Module2 Public strData As String = "Hello from Visual Basic" End Module
In fact, when you declare elements like strData public throughout the program, you need not qualify their names in other code, so I can refer to strData in Module1 as well:
Module Module1 Sub Main() System.Console.WriteLine(strData) End Sub End Module Module Module2 Public strData As String = "Hello from Visual Basic" End Module
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
1. I really enjoyed reading your tutorial. I think there may be some misinterpretation of the bounds of
View Tutorial By: Hydrochloric_Koolaid at 2012-06-27 00:11:28
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net