|
|
Sign In Link to us |
|
Home 1. Introduction 2. Introducing VB 3. Projects 4. Containers 5. Controls 6. Code 7. Forms 8. Debugging Code 8.1 Run, Pause & Stop 8.2 Breakpoints 8.3 Execution Cursor 8.4 Watches 8.5 The Debug Window 8.6 Comments 9. Error Handling 10. Objects 11. User Controls 12. Data Projects 13. API 14. Distribution
|
8. Debugging Code8.1 Execution Run, Pause and Stop
Whilst running the application, you can pause the execution of the code by returning to the Visual Basic design environment and clicking the "Break" button (this looks like a 'pause' symbol on a tape recorder) or by pressing Ctrl + Break on the keyboard. Whilst paused, you can make some changes to the code or inspect the current values of the variables using the Watches window (see below). In some cases, making alterations to the code, even whilst paused, may require the code to re-start. If this happens you will be asked to confirm the change. Finally, clicking the "End" button (this looks like the 'stop' symbol on a tape recorder) will stop all execution and return control back to the Visual Basic design environment. 8.2 BreakpointsA Breakpoint is a marker within the code that tells Visual Basic to automatically pause the execution of the application when it reaches that line of code. Control is returned to the Visual Basic design environment, where you can make some modifications and inspect the current values of variables or Watches (see below). From a Breakpoint you can continue to execute code in real-time or on a step-by-step basis. Breakpoints are a very powerful means of debugging errors within applications.Breakpoints to not form part of the executable code; they are only there as a guide to Visual Basic. If you compile and run your code externally, the Breakpoints that you specify will have no effect.
8.2.1 Adding and Removing Breakpoints
![]() There are two ways to add a Breakpoint: 1. Move the text cursor to the line of code that you want to add the Breakpoint to and press the F9 key on the keyboard. Pressing F9 again whilst on a Breakpoint will remove the Breakpoint. 2. Click the margin adjacent to the line of code that you want to add the Breakpoint to. Clicking here again will remove the Breakpoint. To remove all Breakpoints from the project, press Ctrl + Shift + F9 on the keyboard. 8.3 Execution CursorWhen the code execution is pause (e.g. after a Breakpoint), a highlight is placed over the code to show you the next line of code to be executed.
![]() In the margin adjacent to the execution cursor, a small arrow is used to move the execution. If you want to skip a line of code, you could drag this arrow down past the line and resume execution (or step - see below). Alternatively, you could move the arrow back up to a previous line of code in order to re-run it. When moving the execution cursor, be careful what is skipped and what is re-run as this can give you the impression that your code is functioning incorrectly, when in fact is runs fine - or vice-versa!
8.3.1 Continue, Step Into and Step Over To continue executing the application, click the "Run" button on the top toolbar (or press F5 on the keyboard). This will resume execution from the current line of code and continue as normal, unless another Breakpoint is encountered. If you have added a Breakpoint to a line of code within a loop (eg For..Next or While.Wend), the Breakpoint will pause the code on each iteration of the loop. There are also 2 ways to execute the code on a step-by-step basis:
1. Step Into - press F8 on the keyboard (or select "Step Into" from the "Debug" menu)
2. Step Over - press Shift + F8 (or select "Step Over" from the "Debug" menu) During stepwise execution of code, a highlight is placed over the next line of code to be executed. This shows you the current position of the code cursor.
8.3.2 Using the mouse to display variable values This is very useful when testing parameters being passed to procedures or within loops. 8.4 WatchesA Watch is a monitor that is used to show the current value of a variable within a particular scope (see Scoping Variables). If you needed to check the current value of a particular variable inside a procedure, you could add a Watch on that variable within the scope of that procedure. Whenever the code is paused (e.g. after a Breakpoint), the Watch Window will display the contents of all Watches that you have added. This way you don't need to be looking at that particular piece of code in order to see the values of variables.In this way, several variables can be monitored together and compared to give you a better idea of what may be causing a fault or other undesired effect in your application.
8.4.1 Adding and Removing Watches It is important to note that the dialogue box prompts for an expression as opposed to a variable. This means that you may Watch for results of calculations or parameters returned from functions, as well as just variables. For example, if you were interested in the first 4 characters of a String variable called "strA" you could add a Watch for the whole of "strA", but you could add a Watch for "Left(strA, 4)", which would show the first 4 characters. Similarly, you could add a watch for the total of two Integer type variables; A + B. Set the "Context" for the Watch, depending upon where you want to be able to view it. This is important because you may have several localised variables with the same name, so adding a Watch in 'all procedure' may be confusing. There are other options on the dialogue box that can be used to generate a break (like a Breakpoint) if the expression changes or becomes true. Click "OK" when done. If you haven't added any Watches already or the Watches window is closed, it should appear automatically, listing all of the Watches in the project. The Watches window displays the name of the expression, it's current value and other useful information about each entry.
8.4.2 Interrogating Watches
![]() 8.5 The Debug WindowThe Debug Window is used to send text to during program execution. These messages are not part of the compiled code; they only exist in the Visual Basic design environment. The text can be anything you need to debug your code. For example, if you aren't sure whether a variable is being returned by a function, you could send a message to the Debug Window that displays the values of the variable at different points in the code. The Debug Window is written to by invoking the Print method of the Debug object:
Debug.Print "Book number is:" & lngBookNumber lngBookNumber = GetBookNumber() Debug.Print "Book number changed to:" & lngBookNumberAt the end of the execution, the Debug Window might contain the following: Book number is: 0 Book number changed to: 37The Debug object also has another method called "Assert". This is used to stop the execution if a given condition is false (similar to a Breakpoint). Debug.Assert lngBookNumber <> 0 ' Stop if lngBookNumber is not 0 8.6 CommentsIn Visual Basic, any characters that appear after the inverted comma (') on each line are ignored. This allows you to add in-line comments to your code to help explain how it works. This is especially useful when you have not seen the code for a while or if you are sharing development with other people.You may use as many comments as you like in your code, as they are not compiled by Visual Basic, so have no effect on the final size or performance of the application. | ||