Tuesday 11 March 2014

What are The Types, Scope and lifetime of Variables?

Types, Scope and lifetime of Variables



In Visual Basic variable can be declared in three sections.

  • Procedure.
  • General Section of Form.
  • Module Section of VB.

Procedure Declaration:


When a variable is declared in a procedure ( i.e in subroutine, event handler and function ). The scope and lifetime of that variable will be limited to that procedure in which it is declared. Such variables are considered as local variables. The value of local variable can be preserved between procedure calls if local variable is declared with the Static  keyword.

 Example:

Start Visual Basic with a new Standard EXE project. Place two CommandButton on a Form and changing its Caption according to this diagram.


Write following coding and click event of CommandButton and General Section of Form.

‘ In General Section
  Option Explicit


   Private Sub Command1_Click()
      Print “Used of Static Local Variable”
      Call StaticLocalVarProcedure
End Sub
Sub StaticLocalVarProcedure () ‘ procedure for used of static Variable
      Static a As Integer
      a = a + 1
      print = a
End Sub


   Private Sub Command2_Click()
      Print “Used of Non Static Local Variable”
      Call NonStaticLocalVarProcedure
End Sub
Sub NonStaticLocalVarProcedure ()
      Static a As Integer
      a = a + 1
      print = a
End Sub


Press F5 to run Project and press each CommandButton to check output

General Section Declaration:


When a variable is declared in general section of a Form then it can be called in any procedure which is related with that Form, Such Variables are called Form-wide variable. The Scope and lifetime of such variable is limited to execution of Form.


Example:
                Start Visual Basic with a new Standard EXE Project. Place three CommandButtons controls on a Form and change its Caption property according to diagram.



Write following coding in General Section of Form and Click event of each CommandButton.

   ‘ In General Section of Form
Dim n As Integer

   Private Sub Command1_Click()
                    n = n + 1
End Sub

   Private Sub Command2_Click()
                      Call Increment                   
End Sub
Sub Increment ()
                        n = n + 1
End Sub

   Private Sub Command3_Click()
                      Print “ Value of n = “ ; n
End Sub


Press F5 to run this Project and press each CommandButton to check the output.

Module Declaration:


When a Variable is declared in th module section of Visual Basic, then it can be called in any procedure or function etc. of any application ( i.e. to any procedure of all Forms of current Project ). Such variables  are called global variables and declared with keyword public ( not dim ) in a module e.g.

                               Public n As Integer            ' In module section

Scope and lifetime of global variables limited to the execution of project.



Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

0 comments:

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com