VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 18

Preventing Bugs

This discussion starts by testing your debugging skills by looking at a simple problem in a loop that assigns values to an array of string variables. The array is defined in a module as follows:

Public astrName(9) as String

Because arrays are zero-based by default, this creates an array of 10 variables in the astrName array. Assume that each of the variables is properly assigned a value, but that you later try to display the names in the array using this code:

Dim i as Integer
For i = 1 to 10
MsgBox "Name #" & i & " is " & astrName(i)

As this loop executes, you begin to see a series of message boxes for Name #1, Name #2, and so on. When you reach Name #10, however, a "subscript out of range" error message appears (assuming, that is, that you haven’t used an advanced optimization feature to disable array bounds checking). Obviously, something is wrong, but what is it? After all, you know there are supposed to be 10 variables in that array, and you are only asking to see items 1 to 10. This hardly seems like it should give you any trouble.

This is a simple example, and if you already understand arrays, you can spot the problem without bothering with a watch. The array does have 10 elements, but because it is zero-based, the index values range from 0 to 9, not 1 to 10. When the loop tried to display the value of astrName(10), it was outside the bounds of the array.

The same person who wrote the buggy display loop might also make this mistake in assigning values to the array:

For i = 1 to 9
astrName(i) = "Some name value"

This is a little more insidious than the display loop. This assignment loop doesn’t generate any error messages, but it is wrong nonetheless. If the default Option Base 0 has not been changed, the first element of the array is astrName(0), to which this loop never assigns a value.

A programmer with some VB experience will understand these problems almost at once, but that doesn’t help the beginner who wrote the buggy code in these loops. You can use certain techniques to help prevent these problems by making your intent clearer to others who may later need to maintain your code. Even if no one else but you will ever touch your code, you may benefit from these techniques yourself if you ever have to resume a project that you haven’t touched in a long while.

First, the array might have been defined by explicitly specifying its upper and lower bounds:

Public astrName(0 to 9) as String

This makes it clear that the range of values begins with 0 and ends with 9, so it should be less likely that the flawed assignment loop above will be repeated. It also ensures that there will still be 10 elements in the array even if someone later adds Option Base 1 to the code to change the default lower boundary of an array.

Second, additional care could be taken in the display loop by finding out both the upper and lower boundaries of the array before trying to access its elements:

Dim iLower as Integer, iUpper as Integer
iLower = LBound(astrName)
iUpper = UBound(astrName)
For i = iLower to iUpper
MsgBox "Name #" & i & " is " & astrName(i)

This code is guaranteed to access each element of the array from beginning to end. Nothing gets skipped, and the code can’t step outside the bounds of the array. Remember that you will need to use this technique for safety’s sake if you use the advanced optimization switch that disables automatic array checking (see Chapter 20, "Compiling a VB Application").

You can (and you should!) take preventive measures like these to guard against potential bugs. Even simple safeguards have great value. For instance, it is hard for working VB programmers to imagine coding without being forced to declare variables via Option Explicit, for example, and On Error Resume Next is reserved only for extremely brief routines with their own internal error checks. When you run into a new problem that isn’t so obvious, and you can’t understand what’s wrong just by reading the code, you will develop an instant appreciation for VB’s debugging aids. The first type you will examine is the Watch expression.


<< Previous | Contents | Next >>

Copyright © | All Rights Reserved