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

Event Driven Programming



                                         By default Visual Basic variables are of variant data types. The variant data
                                         type can store numeric, date/time or string data. When a variable is declared,
                                         a data type is supplied for it that determines the kind of data they can store.
                                         The fundamental data types in Visual Basic including variant are integer,
                                         long, single, double, string, currency, byte and boolean. Visual Basic supports
                                         a vast array of data types. Each data type has limits to the kind of information
                                         and the minimum and maximum values it can hold. In addition, some types
                                         can interchange with some other types.


                          4.2 Variables

                          A variable is temporary storage space for numbers, text, and objects. Variables are constantly
                          being created and destroyed and will not hold any values after your program has ended. If you
                          want to save the values of variables or other data. Before allocating storage space for a variable,
                          decide what the variable’s lifetime will be, or in other words, which procedures and which
                          modules should have access to the variable’s value.
                             • Procedure-level variables are created with a Dim statement placed right in the procedure
                               where it’s going to be used. The value of a procedure level variable cannot be accessed
                               outside it’s procedure. When the procedure finishes (End Sub or End Function), the variable
                               is destroyed and memory allocated to the variable is released.
                             • Module-level variables are created with a Private statement in the general declarations
                               section of a Form or code module. The value of the module level variable is available to
                               every procedure in that module. Memory allocated to the module-level variable is not
                               destroyed until the module is Unloaded.
                             • Global variables are created with a Public statement in the general declarations section of
                               a Form or code module. The value of a Global variable is available to any procedure, in
                               any Form or code module. Memory allocated to a Global variable is not released until your
                               program shuts down.
                          It would certainly be easier to make every variable Global. You wouldn’t have to think twice
                          about it’s availability at any given time. But sometimes, every byte of memory counts, so don’t
                          give your variables any more life than they actually require.

                                         A variable name must begin with an alphabet letter and should not exceed
                                         255 characters. It must be unique within the same scope. It should not contain
                                         any special character like %, &, !, #, @ or $.


                          4.3 Variables Declaration

                          Before using variables, you have to set aside memory space for them_after all, that_s what they
                          are, locations in memory. Usually, you use the  Dim statement to declare variables, although you
                          can also use the  Private  (declare a private variable),  Public  (declare a global variable),  Static
                          (declare a variable that holds its value between procedure calls), ReDim  (redimension a dynamic
                          array), or  Type  (declare a user-defined type) keywords to declare variables, as we’ll see in the
                          tasks covered in this chapter.
                          4.3.1 The Dim Statement
                          Here is how you use the  Dim  statement:
                          Dim [WithEvents]  varname[([subscripts])] [As [New]  type] [, [WithEvents]


                          80                     LOVELY PROFESSIONAL UNIVERSITY
   81   82   83   84   85   86   87   88   89   90   91