Understanding Scope in VB.net
By: Steven Holzner in VB.net Tutorials on 2010-11-17
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.
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
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.
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
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.
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception 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
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net