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

Unit 4: VB Programming Fundamentals



               temptation to create a global variable it is much better torefer to the text in the text box this
               way (assuming the name of the dialog form you have created is  Dialog):
               intNumberBalloons = Dialog.TextBox1.Text

               This avoids setting up a global variable needlessly. In fact, one of the most important aspects
               of Visual Basic programming is  variable scope. In general, you should restrict variables to the
               smallest scope possible.

               There are three levels of variable scope in Visual Basic, as follows:
               “  Variables declared in procedures are private to the procedure.
               “ Variables declared at the form or module level in the form or modules (General) section using Dim,
               ReDim,  Private,  Static, or  Type  are form- or module-level variables. These variables are avail-
               able throughout the module.

               “ Variables declared at the module level in the modules (General) section using  Public are global
               and are available throughout the project, in all forms and modules. Note that you cannot use
               Public  in procedures.

                              If you use the  Option Private Module  statement in a module or form, all variables
                              in the module or form become private to the module, no matter how they are
                              declared.


               4.3.5 Implicit and Explicit Variable Declaration
               Declaring a variable tells Visual Basic to reserve space in memory. It is not must that a variable
               should be declared before using it. Automatically whenever Visual Basic encounters a new
               variable, it assigns the default variable type and value. This is called implicit declaration. Though
               this type of declaration is easier for the user, to have more control over the variables, it is
               advisable to declare them explicitly. The variables are declared with a Dim statement to name
               the variable and its type. The As type clause in the Dim statement allows to define the data type
               or object type of the variable. This is called explicit declaration.
               Syntax
               Dim variable [As Type]
               For example,
               Dim strName As String
               Dim intCounter As Integer

               4.3.6 Using Option Explicit Statement
               It may be convenient to declare variables implicitly, but it can lead to errors that may not be
               recognized at run time. Say, for example a variable by name  intcount is used implicitly and is
               assigned to a value. In the next step, this field is incremented by 1 by the following statement
               Intcount = Intcount + 1
               This calculation will result in  intcount yielding a value of 1 as intcount would have been
               initialized to zero. This is because the intcount variable has been mityped as incont in the right
               hand side of the second variable. But Visual Basic does not see this as a mistake and considers
               it to be new variable and therefore gives a wrong result.



                                      LOVELY PROFESSIONAL UNIVERSITY                         85
   86   87   88   89   90   91   92   93   94   95   96