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

Event Driven Programming



                                        The following is an example of an event procedure for a CommandButton that counts
                                        and displays the number of clicks made.
                                        Private Sub Command1_Click ( )
                                        Static Counter As Integer
                                        Counter = Counter + 1
                                        Print Counter
                                        End Sub

                          The first time we click the CommandButton, the Counter starts with its default value of zero.
                          Visual Basic then adds 1 to it and prints the result.


                                        Make a visual basic program use static Variable.


                          4.3.12 Module Level Variables

                          A module level variable is available to all the procedures in the module. They are declared using
                          the  Public or the  Private keyword. If you declare a variable using a Private or a Dim statement
                          in the declaration section of a module—a standard BAS module, a form module, a class module,
                          and so on—you’re creating a private module-level variable. Such variables are visible only from
                          within the module they belong to and can’t be accessed from the outside. In general, these
                          variables are useful for sharing data among procedures in the same module:
                          In the declarative section of any module
                          Private LoginTime As Date ‘ A private module-level variable
                          Dim LoginPassword As String ‘ Another private module-level variable
                          You can also use the Public attribute for module-level variables, for all module types except BAS
                          modules. (Public variables in BAS modules are global variables.) In this case, you’re creating a
                          strange beast: a Public module-level variable that can be accessed by all procedures in the module
                          to share data and that also can be accessed from outside the module. In this case, however, it’s
                          more appropriate to describe such a variable as a property:

                          In the declarative section of Form1 module
                          Public CustomerName As String ‘ A Public property

                          You can access a module property as a regular variable from inside the module and as a custom
                          property from the outside:
                          From outside Form1 module...

                          Form1.CustomerName = “Pradip”
                          The lifetime of a module-level variable coincides with the lifetime of the module itself. Private
                          variables in standard BAS modules live for the entire life of the application, even if they can be
                          accessed only while Visual Basic is executing code in that module. Variables in form and class
                          modules exist only when that module is loaded in memory. In other words, while a form is active
                          (but not necessarily visible to the user) all its variables take some memory, and this memory
                          is released only when the form is completely unloaded from memory. The next time the form


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