Page 93 - Open Soource Technologies 304.indd
P. 93

Unit 4: VB Programming Fundamentals



               Dim is shorthand for dimension and is a rather archaic expression that instructs Visual Basic to
               allocate enough memory to contain the variable that follows the Dim keyword. Therefore, Dim
               i As Integer allocates less memory (2 bytes) than Dim s As Double (8 bytes). There is no way
               to make a variable declared within a procedure visible outside of that procedure.

               The public keyword is used to make a variable visible throughout an application. Public can be
               used only at the module level and cannot be used within a procedure. Usually, the Public
               keyword is used only in standard modules that are not part of a form. Every variable declared
               in the general section of the standard module is public throughout the application unless the
               Private keyword is used. Private restricts the visibility of a variable to the module in which the
               variable is declared.

               4.3.10 Local Variables
               A local variable is one that is declared inside a procedure. This variable is only available to the
               code inside the procedure and can be declared using the Dim statements as given below.
               Dim sum As Integer
               The local variables exist as long as the procedure in which they are declared, is executing. Once
               a procedure is executed, the values of its local variables are lost and the memory used by these
               variables is freed and can be reclaimed. Variables that are declared with keyword Dim exist only
               as long as the procedure is being executed.

               4.3.11 Static Variables
               Static variables are not reinitialized each time Visual Invokes a procedure and therefore retains
               or preserves value even when a procedure ends. In case we need to keep track of the number
               of times a command button in an application is clicked, a static counter variable has to be
               declared. These static variables are also ideal for making controls alternately visible or invisible.
               A static variable is declared as given below:

               Static intPermanent As Integer
               Variables have a lifetime in addition to scope. The values in a module-level and public variables
               are preserved for the lifetime of an application whereas local variables declared with Dim exist
               only while the procedure in which they are declared is still being executed. The value of a local
               variable can be preserved using the Static keyword. The follwoing procedure calculates the
               running total by adding new values to the previous values stored in the static variable value.
               Function RunningTotal ( )
               Static Accumulate
               Accumulate = Accumulate + num
               RunningTotal = Accumulate
               End Function

               If the variable Accumulate was declared with Dim instead of static, the previously accumulated
               values would not be preserved accross calls to the procedure, and the procedure would return
               the same value with which it was called. To make all variables in a procedure static, the Static
               keyword is placed at the beginning of the procedure heading as given in the below statement.

               Static Function RunningTotal ( )





                                      LOVELY PROFESSIONAL UNIVERSITY                         87
   88   89   90   91   92   93   94   95   96   97   98