Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Scope in Visual Basic
In this articleThe scope of a declared element is the set of all code that can refer to it without qualifying its name or making it available through an Imports Statement (.NET Namespace and Type). An element can have scope at one of the following levels:
These levels of scope progress from the narrowest (block) to the widest (namespace), where narrowest scope means the smallest set of code that can refer to the element without qualification. For more information, see "Levels of Scope" on this page. Specifying Scope and Defining VariablesYou specify the scope of an element when you declare it. The scope can depend on the following factors:
Use care when you define variables with the same name but different scope, because doing so can lead to unexpected results. For more information, see References to Declared Elements. Levels of ScopeA programming element is available throughout the region in which you declare it. All code in the same region can refer to the element without qualifying its name. Block ScopeA block is a set of statements enclosed within initiating and terminating declaration statements, such as the following:
If you declare a variable within a block, you can use it only
within that block. In the following example, the scope of the integer variable
Note Even if the scope of a variable is limited to a block, its lifetime is still that of the entire procedure. If you enter the block more than once during the procedure, each block variable retains its previous value. To avoid unexpected results in such a case, it is wise to initialize block variables at the beginning of the block. Procedure ScopeAn element declared within a procedure is not available outside that procedure. Only the procedure that contains the declaration can use it. Variables at this level are also known as local variables. You declare them with the Dim Statement, with or without the Static keyword. Procedure and block scope are closely related. If you declare a variable inside a procedure but outside any block within that procedure, you can think of the variable as having block scope, where the block is the entire procedure. Note All local elements, even if they are Module ScopeFor convenience, the single term module level applies equally to modules, classes, and structures. You can declare elements at this level by placing the declaration statement outside of any procedure or block but within the module, class, or structure. When you make a declaration at the module level, the access level you choose determines the scope. The namespace that contains the module, class, or structure also affects the scope. Elements for which you declare
Private access level are available to every procedure in that module, but not to any code in a different module. The In the
following example, all procedures defined in the module can refer to the string variable
Namespace ScopeIf you declare an element at module level using the Friend or Public keyword, it becomes available to all procedures throughout the namespace in which the element is declared. With the following alteration to the preceding example, the string variable
Namespace scope includes nested namespaces. An element available from within a namespace is also available from within 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 project scope. Choice of ScopeWhen you declare a variable, you should keep in mind the following points when choosing its scope. Advantages of Local VariablesLocal variables are a good choice for any kind of temporary calculation, for the following reasons:
Minimizing ScopeIn general, when declaring any variable or constant, it is good programming practice to make the scope as narrow as possible (block scope is the narrowest). This helps conserve memory and minimizes the chances of your code erroneously referring to the wrong variable. Similarly, you should declare a variable to be Static only when it is necessary to preserve its value between procedure calls. See also
FeedbackSubmit and view feedback for What variable is created inside a function?Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.
What do you call outside variables?External Variables (aka Extraneous Variables or Confounding Variables) are factors that are not manipulated as part of an experiment, but they may exert some influence on the dependent variable under study.
What refers to a region where a variable is actually available for use?Variable Scope is a region in a program where a variable is declared and used.
What is a variable called that is only used in a function and have no meaning outside the function?Local Variables
A local variable is one that occurs within a specific scope. They exist only in the function where they are created. They are sometimes called automatic variables because they are automatically created when the function starts execution, and automatically go away when the function is finished executing.
|