Understanding Scope in VB.net

By: Steven Holzner Emailed: 1642 times Printed: 2113 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The scope of a variable or constant is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where the variable is declared. It's usually a good idea to make the scope of variables or constants as narrow as possible (block scope is the narrowest). This helps conserve memory and minimizes the chances of your code referring to the wrong item. I'll take a look at the different kinds of scope in VB .NET here.

Block Scope

A block is a series of statements terminated by an End, Else, Loop, or Next statement, and an element declared within a block can be used only within that block. Here's what block scope looks like in an example. 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

Procedure Scope

An element declared within a procedure is not available outside that procedure, and only the procedure that contains the declaration can use it. Elements at this level are also called local elements, and you declare them with the Dim or Static statement.

Note also that if an element is declared inside a procedure but outside any block within that procedure, the element can be thought of as having block scope, where the block is the entire procedure.

Module Scope

When discussing scope, Visual Basic uses the term module level to apply equally to modules, classes, and structures. You can declare elements at this level by placing the declaration statement outside of any procedure or block within the module, class, or structure.

When you make a declaration at the module level, the accessibility you choose determines the scope. The namespace that contains the module, class, or structure also affects the scope.

Elements for which you declare Private accessibility are available for reference to every procedure in that module, but not to any code in a different module. The Dim statement at module level defaults to Private accessibility, so it is equivalent to using the Private statement. However, you can make the scope and accessibility more obvious by using Private. In this example, I've declared Function1 as private to Module2, so it's inaccessible in Module1 (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

Namespace Scope

If you declare an element at module level using the Friend or Public statement, it becomes available to all procedures throughout the entire namespace in which it is declared. Note that an element accessible in a namespace is also accessible from inside any namespace nested inside that namespace.

Note 

If your project does not contain any namespace statements, everything in the project is in the same namespace. In this case, namespace scope can be thought of as procedure scope.


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
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

More Latest News
Most Viewed Articles (in VB.net )
Sub Procedures and Functions in VB.net
Handling Strings in VB.net
Arrays and Dynamic Arrays in VB.net
For Each…Next Loop in VB.net
While Loop in VB.net
How to export from database to excel using VB.net
How to export from DataGridView to excel using VB.net
DataSet Class in VB.net
Scope in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
What is .NET Framework and the Common Language Runtime?
Visual Basic Statements
Data types in VB.net
Operators in VB.net
If…Else Statements in VB.net
Most Emailed Articles (in VB.net)
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Multi threaded Socket Programming in VB.net
Nested If and Single line if statement
Format Excel 2007 Page in VB.NET
Stack data structure in VB.net
File directory operationst in VB.net
Vb.net binary reader and writer
Using Excel 2007 Chart in VB.NET 2005
How to create an XML file in VB.net
A tutorial on Chat Server and Chat Client in VB.net
Structured Exception Handling in VB.net
Properties in VB.net
“Using If with And” and Comparing two integers using If
Using Select Case in VB.net
For Loop in VB.net