Tuesday, April 26, 2011

Variable Declaration -1 . Default


Dim variablename
Variables declared with the Dim statement within a procedure exist only as long as the procedure is executing. When the procedure finishes, the value of the variable disappears. In addition, the value of a variable in a procedure is local to that procedure — that is, you can't access a variable in one procedure from another procedure. These characteristics allow you to use the same variable names in different procedures without worrying about conflicts or accidental changes.

Static Variables declaration

Static <variablename >

Variables have a lifetime, the period of time during which they retain their value. The values in module-level and public variables are preserved for the lifetime of your application. However, local variables declared with Dim exist only while the procedure in which they are declared is executing. Usually, when a procedure is finished executing, the values of its local variables are not preserved and the memory used by the local variables is reclaimed. The next time the procedure is executed, all its local variables are reinitialized.
However, you can preserve the value of a local variable by making the variable static. Use the Static keyword to declare one or more variables inside a procedure, exactly as you would with the Dim statement:
Eg
Static Depth 

If variables are not implicitly or explicitly typed, they are assigned the variant type by default.  The variant data type is a special type used by Visual Basic that can contain numeric, string, or date data.

     
Private Sub Command1_Click()
    Dim ProName
    Dim Quantity, ProCode, Slaesprice
    ProName = "Book"
    Quantity = 200
    ProCode = "Al001"
    Salesprice = 2000.75
    Debug.Print ProName; Quantity, ProCode, Salesprice
End Sub

Output
Book 200      Al001          2000.75


No comments:

Post a Comment