
When you declare a variable, you should keep in mind the following points when choosing its scope. Public elements in a module, class, or structure are also available to any project that references their project. In this case, namespace scope can be thought of as project scope. If your project does not contain any Namespace Statements, everything in the project is in the same namespace. An element available from within a namespace is also available from within any namespace nested inside that namespace. Namespace scope includes nested namespaces. ' Include this declaration at module level (not inside any procedure).
#CSCOPE FOR WINDOWS TUTORIAL CODE#
With the following alteration to the preceding example, the string variable strMsg can be referred to by code anywhere in the namespace of its declaration. If 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. StrMsg = "This variable cannot be used outside this module." ' Put the following Sub procedure in the same module. ' Put the following declaration at module level (not in any procedure). When the second procedure is called, it displays the contents of the string variable strMsg in a dialog box. In the following example, all procedures defined in the module can refer to the string variable strMsg. However, you can make the scope and access level more obvious by using the Private keyword in the Dim statement. The Dim statement at module level defaults to Private if you do not use any access level keywords.

The namespace that contains the module, class, or structure also affects the scope.Įlements for which you declare Private access level are available to every procedure in that module, but not to any code in a different module. When you make a declaration at the module level, the access level you choose determines the scope. 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. Module Scopeįor convenience, the single term module level applies equally to modules, classes, and structures. You cannot declare any element using the Public keyword within a procedure.

If n < 1291 ThenĪll local elements, even if they are Static variables, are private to the procedure in which they appear. In the following example, the scope of the integer variable cube is the block between If and End If, and you can no longer refer to cube when execution passes out of the block. If you declare a variable within a block, you can use it only within that block. Block ScopeĪ block is a set of statements enclosed within initiating and terminating declaration statements, such as the following: All code in the same region can refer to the element without qualifying its name.

Levels of ScopeĪ programming element is available throughout the region in which you declare it. For more information, see References to Declared Elements. Use care when you define variables with the same name but different scope, because doing so can lead to unexpected results. The access level you declare for the element The namespace containing the element's declaration The region (block, procedure, module, class, or structure) in which you declare the element The scope can depend on the following factors: You specify the scope of an element when you declare it. For more information, see "Levels of Scope" on this page. 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. An element can have scope at one of the following levels: LevelĪvailable only within the code block in which it is declaredĪvailable to all code within the procedure in which it is declaredĪvailable to all code within the module, class, or structure in which it is declaredĪvailable to all code in the namespace in which it is declared The 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).
