1.            Debugging a programme

Breakpoints Exercise Practice

Learning Outcomes

On completion of this chapter you will know:

·         The nature of breakpoints

·         How to set and remove breakpoints in a programme

·         How to step through a programme line by line

·         How to examine values of variables while stepping through a programme

Download pdf version

Introduction

When normally running a programme the processing speed is very fast and we cannot see the individual lines of programme being executed. This is as it should be since if we could see each line being executed the programme would be running so slow that it would be completely useless. Occasionally, however, it is good to be able to slow down the processing so we can examine individual lines being executed. Some of the reasons for this are:

The programme may not be functioning as it should be due to faulty code. In this case it is good to be able to slow down the processing so we can examine each command being executed and check the values of the variables as the data in them changes.

For learners, being able to see the execution sequence in such instances as which lines are skipped and which lines are executed in a If..Else or Select..Case construct or which group of lines are repeated in a loop construct would be helpful in the understanding of those constructs.

In this chapter we shall concentrate how to examine the line by line execution of the programme code as well as checking the values of the variables at each stage of the execution.

Breakpoints

Go to top

When we manually want to run a Visual Basic programme we simply click inside the code and then press F5. The programme will then run, executing one line after another at the rate of perhaps 500,000 lines per second until the programme finishes. The only interruptions to this would be if we used either an InputBox or Msgbox command in the code. In this case the execution would stop until we clicked the OK button on the dialogue boxes. Even though the programme execution would be halted by the commands mentioned we would not still be able to examine the code. In order to be able to do this we need to set a breakpoint in the programme before we run it.

For our example we shall use the first programme that we wrote in Listing 21. in order to examine the code. We first list the programme itself as shown in Figure 91. We want to start examining the code at the line for calculating the gross. To do this we move to the grey area to the left of the edit area and click opposite the line for calculating the gross.  This will result in a dark red circle to appear in the grey area and the line of code itself will be highlighted in the same colour as shown below in Figure 92. Our breakpoint is now positioned at this line. This means that the programme will run normally until it reaches the breakpoint. It will then stop and at this point we can examine the values of the variables that have been given values up until then.

Figure 91

Figure 92

In order to try this out we simply press F5 as before. We shall be asked for the value of the hours in the first line of code and of the rate in the second line of code. After this the breakpoint will be reached. The execution of the programme will pause and the line for calculating the gross will be highlighted in yellow as shown in Figure 93.

 

Figure 93

The highlighted line has not yet been executed and thus we can only examine the values of the variables sngHours and curRate. To examine the value of a variable you simply position the cursor over its name. In Figure 94 the cursor has been positioned over sngHours and a small textbox appears below it, showing us its value. If we were to position the cursor over curGross, we would see that it has a value of zero because the highlighted line has not yet been executed and therefore the value of the gross has not been calculated.

Figure 94

To execute the current line we press F8. This causes the execution of the line to occur and the yellow highlight will move on to the next line, indicating that it is now the line that is ready for execution. This is shown in Figure 95 below. Since the line for calculating the gross has been executed, when we put our cursor over curGross we can read its updated value.

Figure 95

Pressing F8 again will cause the line for calculating the tax to be executed and will cause the highlight to move to the line for calculating the nett. This is shown in Figure 96 where we can read the value of the variable curTax.

Figure 96

Similarly pressing F8 once more will execute the line for calculating the nett and allow us to check the value for curNett as shown below.

Figure 97

Practice

Go to top

Copy Listing 71 into your code module and this time debug it instead of running it. To do this take the following steps:

1.      Insert a breakpoint at line 9 - the first executable line.

2.      Press F5 to start the programme running

3.      To make the programme move from line to line press F8

Notice the control jumping to the functions and then returning back to the line that initially called the functions.  Also ensure that you check the values calculated within the function bodies and also check that those values are passed back to the appropriate variables in the main programme..

Exercise

Go to top

Debug the programme you created for Exercise 51 in the same way as you debugged Listing 71