Monday 23 December 2013

Lifetime of Variables


Lifetimeof Variables
Lifetime of a Variable : It is the period for which they retain their value. Variables declared as Public exist for the lifetime of the application. Local variables, declared within procedures with the Dim or Private statement, live as long as the procedure.
You can force a local variable to preserve its value between procedure calls with the Static keyword. The advantage of using static variables is that they help you minimize the number of total variables in the application.
In addition to scope, variables also have a lifetime, which is the time during which a variable retains its value. The values in module-level and public variables are preserved as long as the database is open (unless you reinitialize your code).
You can force a local variable to preserve its value between procedure calls with the Static keyword. The advantage of using static variables is that they help you minimize the number of total variables in the application.

Variables declared in a Form outside any procedure take effect when the Form is loaded and cease to exist when the Form is unloaded. If the Form is loaded again, its variables are initialized, as if it’s being loaded for the first time
This is true for form and report module-level variables even if you close the form or report. However, local variables declared with the Dim keyword exist only while the procedure in which they are declared is running. Usually, when a procedure has finished, the values of its local variables are discarded and the memory used by the local variables is reclaimed. The next time the procedure is run, all of its local variables are reinitialized. However, you can make Visual Basic preserve the value of a local variable by making the variable static.

To make a local variable in a procedure static, use the Static keyword to declare the variable exactly as you would using the Dim statement.

Static intX As Integer

For example, the following function calculates a running total by adding a new value to the total of previous values stored in the static variable dblAccumulate.

Function RunningTotal(ByVal dblNum As Double) As Double
   Static dblAccumulate As Double
   dblAccumulate = dblAccumulate + dblNum
   RunningTotal = dblAccumulate
End Function

If you declare dblAccumulate with the Dim keyword instead of the Static keyword, the previous accumulated values aren't preserved across calls to the function, and the function simply returns the value with which it was called.

You can produce the same result by declaring dblAccumulate in the Declarations section of the module, making it a module-level variable. However, after you change the scope of a variable in this way, the procedure no longer has exclusive access to that variable. If other procedures access the value of the variable and change it, the running totals would be unreliable.

To make all local variables in a procedure static, place the Static keyword at the beginning of a procedure declaration, as shown in the following example:

Static Function RunningTotal (ByVal dblNum As Double) As Double
This makes all the local variables in the procedure static, regardless of whether they are declared with the Static or Dim keywords, or declared implicitly. You can place the Static keyword in front of any Function or Sub procedure heading, including event procedures and those that are also declared as Private.

No comments:

Post a Comment