Procedure Scope Variables

Procedure Scope Variables

These are variables declared within a procedure.  Other procedures may not interact with them, either to read their value or change value.  When the procedure has completed execution, the variables' values are lost.

                       

In the Sub Form1_Load the local variable intColour is accessible only within Form1_Load.  If another Sub attempts to use intColour, VB.NET will not know its value and treat it as a new variable.

There are circumstances in which you may want a local variable's value to be preserved after execution of the Sub has completed.  This is useful when the application will be re-entering the Sub many times.  The Static keyword is used instead of the Dim keyword in that event.

In this example intCount maintains the sum of the numbers entered into the TextBox control each time the button is clicked.

 

An equivalent method would be to declare a class scope variable.  In fact the MSIL converts a Static variable into a class scope variable.

You cannot declare any variables with the Public keyword within a Sub.

Block Scope

A block scope variable is accessible only within the code block which it is declared. 

 

A block is a set of statements enclosed within initiating and terminating declaration statements, such as the following:

  • Do and Loop
  • For [Each] and Next
  • If and End If
  • Select and End Select
  • Try and End Try
  • While and End While
  • With and End With

Module Scope

The term module scope applies equally to modules, classes, and structures.  Elements declared within a class but outside any procedure are module scope.  The Dim statement at module scope defaults to Private if you do not use any access level keywords.  Elements declared Private are accessible to all procedures within the class but not any procedures in other classes.

Namespace Scope

If you declare an element at module level using the Public or Friend keyword, it becomes available to all procedures throughout the namespace in which the element is declared.

Choice of Scope

Local variables have the advantage of better memory usage.  Their memory is released when the procedure completes its execution.  In addition, local
variable names are not susceptible to name conflict.  Each procedure may have its own variable with the same name but they are separate.

In general, when declaring any variable, it is good programming practice to make the scope as narrow as possible (block scope is narrowest).  This helps to conserve memory.

Resolving a Reference When Multiple Variables Have the Same Name

When the compiler attempts to match a name reference to a name declaration, it looks for the matching declaration with the narrowest scope. This means it starts with the code making the reference and works outward through successive levels of containing elements.

If you want to override this search process and specify a name declared in a broader scope, you must qualify the name with the containing element of the broader scope. In some cases, you might also have to qualify the containing element.

 

 

 

 

posted @ 2015-06-17 00:03  xymum  阅读(127)  评论(0编辑  收藏  举报