Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.

Static Declaration

If you want a local variable to preserve its value for the duration of the form, not just of the procedure, you can use the Static statement for the declaration. When you declare a variable with the Static statement, the value of that variable will be preserved and will not be reinitialized between each call of the procedure. The following example illustrates the difference between a variable declared with a Dim statement and one with a Static statement:

Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click Dim I as Integer Static J as Integer I = I + 1 J = J + 1 MsgBox( “I = “ & I & “. J = “ & J) End Sub

When you run this program and then click on the form repetitively, you will notice that the message box continues to display 1 for I, but increases the value for J each time.

Scope of Static Variables

If a static variable lives as long as the form, how then is it different from a class level variable? They differ in scope. A static variable, which can be declared only inside a procedure, is a local variable, recognized only in the procedure. On the other hand, a class level variable is recognized by all procedures in the same form. For example, suppose you have a form with a button named btnTest. Consider the following code:

Dim K as Integer ‘Place this line right after the Class statement Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click Static J as Integer K = K + 1 J = J + 1 MsgBox( “K = “ & K & “. J = “ & J) End Sub

You should be able to enter and run the routine without any problem. If you then enter the following code, however, you will notice that although you can enter the first MsgBox statement without any problem, the second MsgBox will have its variable J underlined. The compiler will tell you that J is not declared because a Static variable in another procedure is local to that procedure, and is not recognized in any other procedure.

Private Sub btnTest_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click MsgBox(“K = “ & K) MsgBox (“J = ” & J) End Sub 

Block Level Declaration

Procedure level declarations can be placed anywhere in the procedure before the variables in question are used. If you declare a variable within an If block, however, the variable will be recognized only within the block. After the execution leaves the block, the variable is out of scope. If you subsequently attempt to use it, the compiler will tell you that it is not declared.

Overlapping Declarations in Form and Procedure

What happens when a class level variable is also declared in a procedure? The one declared in the procedure is a different variable, a local variable with the same name. VB recognizes the variable of the narrowest scope. This procedure recognizes the local variable, not the class level variable. To reference the variable declared at the class level, qualify the variable with Me. The following code example shows how the variable I declared both at the class level and a procedure can be referenced in the procedure:

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click Dim I As Integer I = 3 Me.I = 12 MsgBox(I & ", " & Me.I) End Sub

Run the project. You should see that the procedure displays 3 and 12.

Note, however, that you cannot have overlapping declarations in procedures and blocks; if you declare a variable in a procedure, you cannot declare another variable with the same name inside a block, such as an If block, in that procedure.

Last change: February 13 2016 18:47:36.