Guaranteed Hits to your web site!












Learn Visual Basic 6.0


1. Introduction to the Visual Basic Language and Environment


Variable Declaration

  1. There are three ways for a variable to be typed (declared):
  2. 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.

  3. To implicitly type a variable, use the corresponding suffix shown above in the data type table. For example,


    creates a string variable, while


    creates an integer variable.

  4. There are many advantages to explicitly typing variables. Primarily, we insure all computations are properly done, mistyped variable names are easily spotted, and Visual Basic will take care of insuring consistency in upper and lower case letters used in variable names. Because of these advantages, and because it is good programming practice, we will explicitly type all variables.

  5. To explicitly type a variable, you must first determine its scope. There are four levels of scope:
  6. Within a procedure, variables are declared using the Dim statement:
    Procedure level variables declared in this manner do not retain their value once a procedure terminates.

  7. To make a procedure level variable retain its value upon exiting the procedure, replace the Dim keyword with Static:
  8. Form (module) level variables retain their value and are available to all procedures within that form (module). Form (module) level variables are declared in the declarations part of the general object in the form's (module's) code window. The Dim keyword is used:
  9. Global level variables retain their value and are available to all procedures within an application. Module level variables are declared in the declarations part of the general object of a module's code window. (It is advisable to keep all global variables in one module.) Use the Global keyword:
  10. What happens if you declare a variable with the same name in two or more places? More local variables shadow (are accessed in preference to) less local variables. For example, if a variable MyInt is defined as Global in a module and declared local in a routine MyRoutine, while in MyRoutine, the local value of MyInt is accessed. Outside MyRoutine, the global value of MyInt is accessed.

  11. Example of Variable Scope:

    Module1
    Global X as Integer

    Form1
    Dim Y as Integer

    Sub Routine1()
      Dim A as Double
      .
      .
    End Sub


    Sub Routine2()
      Dim B as Double
      .
      .
    End Sub


    Form2
    Dim Z as Integer

    Sub Routine3()
      Dim C as String
      .
      .
    End Sub

    Procedure Routine1 has access to X, Y, and A (loses value upon termination)
    Procedure Routine2 has access to X, Y, and B (retains value)
    Procedure Routine3 has access to X, Z, and C (loses value)


Pen Line


Counter Hit Counter Hit


This Homepage is special brought to you by CK Tan
Hosted by www.Geocities.ws

1