This article covers the concept of Variable Scope in VB.NET
There are two types of variable scope in VB.NET. The first is the local scope, which exists within a local body like a function, and the second is the global scope which exists throughout the VB.NET program’s main body.
Local scope refers to the scope existing within a code block, function, procedure, class etc. Declaring a variable within any of these makes the variable accessible only within that scope. In a way, this variable is now “local” to that region where it was declared.
Below are some examples of local scopes in VB.NET.
msg has been declared in the procedure
disp. The variable
msg is now in the local scope of the
disp and cannot be accessed by anything outside this procedure.
Public Sub disp() Dim msg As String = "Hello World" Console.WriteLine(msg) End Sub
Below is an example we pulled from our VB.NET classes article. These three variables are only accessible in the class
Class Person Private name As String Private age As Integer Private RollNo As String End Class
Below is image of when we tried to access the variable
name. You will be unable to access the variable name regardless of whether you try to print out
Read the Access modifier section at the end of this article to learn about this.
Global variables are those declared in the main method. Any variable declared here will be accessible through the program code, even by other methods.
Module Module1 Public Sub disp() Console.WriteLine(msg) End Sub Sub Main() Dim msg As String msg = "Hello World" disp() End Sub End Module
Despite not declaring
msg in the procedure
disp and not assigning it a value in the procedure, assigning
msg values in the
main() method body and calling the function displays the output as shown below. This is due to the nature of global variables.
However, it’s best to limit doing this as much as possible as it can get messy if you’re not careful.
VB.NET has several keywords which can be used to change or assign a protection levels to either variables of methods. For instance, you can make a local variable available to the global namespace by the using the
Public keyword. These are referred to Access Level in VB.NET. The keywords which change Access Levels are called Access Modifiers.
Public: Public elements are accessible to all other elements in VB.NET. Furthermore they can be accessed from other projects as well. Below is an example of us declaring Public element.
Public Class ClassForEverybody
You can’t declare global variables in VB.NET using Access Modifiers, but you can try an alternative approach where you declare variables in a Global class. See this stack overflow answer for more information.
Private: Privates elements are those which can only be accessed within the class that they were declared in. Even if a class with Private elements is inherited by another class, the child class will be unable to access these Private elements such as variables and methods.
Private x as Integer
Protected: Protected elements are the same as Private elements, except that a child class can access them if it inherits them.
Protected x as Integer
In general, when declaring any variable or constant, it’s good practice to keep the scope as narrow as possible (block scope is the narrowest). This helps conserve memory resources and minimizes the chances of your code referring to the wrong variable by accident.
This marks the end of the VB.NET Variable Scope Article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be directed to the comments section below.