Changes

Declaring Visual Basic Variables and Constants

24 bytes added, 18:52, 1 August 2007
Understanding Variable and Constant Scope
There are four levels of scope in a Visual Basic application:
* === Block Level Scope===
When a variable or constant is declared in a distinct code structure (such as an ''If'' statement or ''Do loop''), the variable is only visible and accessible to the code within that structure. For example, in the following code the ''intCustCount'' variable is only accessible to code within the ''If'' statement:
</pre>
* === Procedure Level Scope===
Variables and constants declared within a Visual Basic procedure (i.e a Function or Subroutine) are only visible and accessible to code within that procedure (for details of Visual Basic procedures see [[Visual Basic Modules and Procedures]]. For example, the variable ''intResult'' in the following code is only visible to code within the function:
</pre>
* === Module Level Scope===
When a variable or constant is declared outside of any procedures or code structures in a Module it is deemed to have module level scope. This means that the variable or constant is visible to all Visual basic code contained in the same module, regardless of whether that code is located in a procedure or not. There is a section at the top of each module called the ''Declarations Section'' for this purpose. The Declaration section is located above all procedures and code in a module.
* === Global Scope===
When a variable or constant is declared as ''global'' it is visible to all procedures and modules that comprise the application. Global variables and constants, as with Module level variables, must be declared outside of any procedures in teh Declarations section and must use the ''Public'' keyword. The syntax for declaring a global variable is as follows: