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.

Procedure-Level Declaration

Variables and constants declared in a procedure (within the Sub … End Sub structure) are recognizable only in that procedure. They are said to be the procedure level or local variables and constants. The same names used in other procedures refer to different memory locations, and have nothing to do with those in the current procedure. Because these variables are independent of each other, you can declare different data types in different procedures using the same name. For example, it is legitimate to have the following declarations for I:

Private Sub btnTest1_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnTest1.Click Dim I as Integer ‘ additional code lines End Sub
Private Sub btnTest2_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnTest2.Click Dim I as Long ‘ additional code lines End Sub

Such a practice creates confusion, however, even for yourself when you review the code later. Avoid this practice by all means.

Lifetime of Variables Declared in a Procedure with Dim

Variables declared with a Dim statement in a procedure exist as long as the procedure is in action. When the procedure ends, these variables are said to be out of scope and disappear. When the procedure is called again, these variables are reinitialized. They no longer have their previous values. They will be reset to zero if they are numeric variables, or to a zero-length string if they are string variables.

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