Debugging features of MS Visual Basic and C

Nota bene:

Again, this is an old assignment I did about a decade ago for a HNC Software Engineering course I did. Useful if only for historic reason, but much of it still holds true.

Brief comparison and contrast of the Debugging features of MS Visual Basic and C

Overview of C debugging

C allows you to single step through programs, watch variables change as the program runs, execute sections of the program at full speed and stop where you want, and change the value of variables and expressions in the middle of a program.   The debugger is built into the integrated development environment (IDE)

Error messages
On compilation as errors will appear in a Message window at the bottom of the screen. The message window will tell you the type of error detected (declaration syntax error, function call missing…)  It will highlight the line(s) of code and allow you to switch to the editing part of the program to correct them. Depending on the compiler, a marker will indicate the place in the program line where the error was first detected.

Single stepping
This allows you to step through the program, line at a line. It is particularly useful in tracking errors in loops and If statements, for example finding the Else statement in the wrong place with nested conditions where you can stop which lines are skipped.

Watch Expressions
Used with single-stepping, these can let you see how the value of a variable changes as the program runs, stopping errors this way. For example, watching the variables Number1,
Number2 and Answer.

Breakpoints
For larger programs and big loop, single stepping and watch expressions can be time consuming and tedious, particularly if you believe the error lies further on in the code. Here’s where breakpoints help.

You start the program and it executes all the statements up to the breakpoint, then stops. You can then examine the state of the variables at that point using the Watch window. As many breakpoints as you want can be added. As an example, our breakpoint at line 15 catch that Answer is –6,543 as the cause is deduced to be exceeding the capacity of the assign Int. Reassigning as Long Int cures this instance.

Evaluation Window
This can be used to change the values of variables while a program is running. Useful if you’ve single stepped into a program and realized the variable is out, but want to see what happens to the rest of the program had it been correct.  It can also be used if you want to generate values to test the operation of particular routines without inputting these values into the program, or to find the values of expressions

(i.e. evaluate Answer * 3 ).  One common use for this debug tool is to find the values of array elements and the contents of points (i.e. Results[5], *(ptr+7) ).  Functions, however, cannot be handled this way.

Step Over
With the addition of function calls, things can get more complex.  Instead of single-stepping into (or tracing into) a function you can, if you desire, execute it as a single line of code – or step over the function. This is useful if you know that the functions are not the source of problems and you don’t want to waste time running through them unnecessarily.

Call Stack Option
Here, as opposed to stepping over, you may want to delve into several levels of functions – and get lost. The call stack option presents a handy way to see the whole chain of function calls that led to your present location in the program.  Really it’s just to snapshot, a quick tree to tell you where you are, though if a call to a function has arguments, the values of these will be shown in the call stack window.

End.

Overview of Visual Basic Debugging

Like C, Visual Basic allows you to single step through programs, watch variables change as the program runs, execute sections of the program at full speed and stop where you want, and change the value of variables and expressions in the middle of a program.

Error messages
On compilation as errors will appear in a Debug Window at the bottom of the screen. The message window will tell you the type of error detected (declaration syntax error, function call missing…).   During run-time you can use it to print internal values without printing them to the actual form.

Immediate Pane.
Similar to the Evaluation Window in C, this is where you can enter code to execute it immediately, one line of code at a time. In break mode a statement in the Debug window is executed in the context displayed, i.e. if you type Print variable name your output is the value of that local variable.  This is the same as if the Print method had actually occurred in the procedure you were executing when the program halted.

Watch Pane
Like C, used with single-stepping, these can let you see how the value of a variable changes as the program runs, stopping errors this way. The Watch pane is located directly below the Debug window title bar.  The information shown includes the expression, its context and its value at it enters break mode (below). If the context of the expression is not in scope (when going into break mode), the current value is not shown.

Break Mode
Again, similar to C. For larger programs and big loop, single stepping and watch expressions can be time-consuming and tedious, particularly if you believe the error lies further on in the code. Here’s where break points help. You start the program and it executes all the statements up to the break point, then stops. You can then examine the state of the variables at that point using the Watch Pane, above.

Call Command
Like Call Stack Option in C.  Available only during break mode, it allows a procedure call list of functions.

Tracing Execution
Tracing execution is useful in Visual Basic because in event-driven programming, it may not be obvious which statement is executed first. Like Step Over in C, you can use this to step across procedure calls rather than into the called procedure.

(Assignment) Analyse the following program for errors

The program is a recursive procedure that counts from 7 to zero, at first sight the main routine progresses logically. The use of Call stack indicates progress of the procedures. Step through shows progress within the procedures. Debug.print for the selected variable shows the area of the error. “Add watch” also is a good indicator of the status of each of the variables.

Original Code

Private Sub Form_Click()
Dim numberoftimes As Integer
numberoftimes = 7
decrement (numberoftimes)
numberoftimes = 7
writealine (numberoftimes)
End Sub

Private Sub Form_Load()
Label1.Caption = "Click the Form to Run"
End Sub

Sub decrement(Index)
Index
= Index - 1
If Index > 0 Then
writealine (Index)
End If
End Sub

Sub writealine(count)
Form1.Print "The value of the count is " + Str(count)
Debug.Print count
decrement (count)
End Sub 2.

Fixed Code

Private Sub Form_Click()
Dim numberoftimes As Integer
numberoftimes = 8
rem monitored Index count in Intermediate window to find fault
decrement (numberoftimes)
numberoftimes = 7
writealine (Index)
rem above changed
End Sub

Private Sub Form_Load()
Label1.Caption = "Click the Form to Run"
End Sub

Sub decrement(Index)
Index = Index - 1
If Index > 0 Then
writealine (Index)
Debug.Print Index
rem above added
End If
End Sub

Sub writealine(count)
Form1.Print "The value of the count is " + Str(count)
rem Debug.Print count
decrement (count)
End Sub

The routine below is a Swap routine containing errors. Use debugging skills to create a working program.

Original Code

Private Sub Form_Click()
Dim firstnumber, secondnumber, temp As Integer
firstnumber = Text1.Text
If firstnumber > secondnumber Then
secondnumber = temp
secondnumber = firstnumber
temp = firstnumber
Form1.Print "numbers in descending order are"
Form1.Print "Lowest is " + Str(firstnumber)
Form1.Print "Highest is " + Str(secondnumber)
Else
Form1.Print "numbers do not need sorting"
End If
End Sub

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Label1.Caption = "The program will display in descending order the two numbers you have entered"
End Sub

Corrected Code

Private Sub Form_Click()
Dim firstnumber, secondnumber, temp As Integer
firstnumber = Text1.Text
secondnumber = Text2.Text
Rem 1. Added above line. Now displays second number
Rem and states when numbers don’t need sorting

If firstnumber > secondnumber Then
secondnumber = temp
firstnumber = secondnumber
Rem 2. Corrected 'obvious' error
Rem secondnumber was = temp AND =firstnumber
Rem transposed to correct

temp = firstnumber

Form1.Print "numbers in descending order are"
Form1.Print "Lowest is " + Str(Text1.Text)
Form1.Print "Highest is " + Str(Text2.Text)
Rem amended the Str from firstnumber to Text1.Text to get entered variables
Else
Form1.Print "numbers do not need sorting"
End If
End Sub

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Label1.Caption = "The program will display in descending order the two numbers you have entered"
End Sub

Rem Used watch point on Firstnumber to track firstnumber and locate certain faults.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.