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 5: Decision
Last change: February 13 2016 18:47:58.

Additional Notes

The following notes provide additional information pertaining to topics discussed in chapter 4.

Block Level Declaration

Declaration of variables at the block level was discussed in Chapter 4, “Data, Operations, and Built-In Functions.” It was mentioned that variables declared at this level have a block scope. But what constitutes a block in this context? In the case of the If structure, each of the If, ElseIf, and Else blocks is independent of each other, so the following declarations pose no problem.

If A = B Then Dim I As Integer . ElseIf A > B Then Dim I As Integer . Else Dim I As Integer . End If

Each of the variables named I is separate and distinct from the other variables of the same name; when the execution leaves the block where I is declared, it will no longer be recognized. Note, however, you cannot declare I in these blocks if you have declared a procedure level variable with the same name. By the same token, if you have inner blocks in one of these blocks, you will not be able to (and should not) declare another I.

These same rules apply to the Select Case structure. You can declare the same variable in each of the mutually exclusive Case blocks, but you cannot make any of these declarations if you have already declared a variable of the same name at the procedure level, nor can you declare the same variable in an inner block (either Case or If block).

TypeOf Operator versus TypeName

As discussed in chapter 4, both the TypeOf operator and the TypeName function deal with data type determination. When needed, you should find that the TypeName function can be handily used in the Select Case structure. For example, if your code will vary depending on the type of a variable, A, you can code:

Select Case TypeName(A) Case “Integer” . Case “String” . . End Select

You just cannot use the TypeOf operator in the above Select Case statement.

On the other hand, the TypeOf operator will be a better choice for an If statement. For example the code If TypeOf(A) Is String will execute faster than If TypeName(A) = “String” because the latter involves string comparison.


Last change: February 13 2016 18:48:00.