Visual Basic Concepts
Tips for Debugging
There are several ways to simplify debugging:
When your application doesn't produce correct results, browse through the code and try to find statements that may have caused the problem. Set breakpoints at these statements and restart the application.
When the program halts, test the values of important variables and properties. Use Quick Watch or set watch expressions to monitor these values. Use the Immediate window to examine variables and expressions.
Use the Break on All Errors option to determine where an error occurred. To temporarily change this option, select Toggle from the Code window context menu, then toggle the option from the submenu. Step through your code, using watch expressions and the Locals window to monitor how values change as the code runs.
If an error occurs in a loop, define a break expression to determine where the problem occurs. Use the Immediate window together with Set Next Statement to re-execute the loop after making corrections.
If you determine that a variable or property is causing problems in your application, use a Debug.Assert statement to halt execution when the wrong value is assigned to the variable or property.
To set the error trapping state that Visual Basic defaults to at the beginning of any debugging session, open the Options dialog box (available from the Tools menu), select the General tab, and set the Default Error Trapping State option. Visual Basic will use this setting the next time you start it, even if the setting was entered for another project.
Occasionally you may encounter a bug that’s especially difficult to track down. Don’t panic – here are some things that you can do:
First and foremost, make a backup. This is the point at which even experienced programmers frequently lose many hours of work. When you experiment, it is far too easy to accidentally overwrite or delete necessary sections of code.
Use the debugging facilities built in to Visual Basic. Attempt to identify the line or lines of code generating the error. Isolate the code. If you can isolate the problem to one block of code, try to reproduce the same problem with this block of code separated from the rest of your program. Select the code, copy it, start a new project, paste the code into the new project, run the new project, and see if the error still occurs.
Create a log file. If you cannot isolate the code or if the problem is erratic or if the problem only happens when compiled, then the debugging facility of Visual Basic will be less effective. In these situations you can create a log file which records the activity of your program. This will allow you to progressively isolate the location of the suspect code. Call the following procedure from various points in your program. You should pass in a string of text which indicates the current location of the code executing in your program.
Sub LogFile (Message As String) Dim LogFile As Integer LogFile = FreeFile Open "C:\VB\LogFile.Log" For Append As #LogFile Print #LogFile, Message Close #LogFile End Sub Sub Sub1 () '... Call LogFile("Here I am in Sub1") '... End Sub
Simplify the problem. If possible, remove any third party controls and custom controls from your project. Replace them with Visual Basic standard controls. Eliminate any code that does not seem to relate to the problem.
Reduce the search space. If you cannot resolve the problem with any of the above methods, then it is time to eliminate all other non-Visual Basic causes from the problem search space. Copy your AUTOEXEC.BAT and CONFIG.SYS files to backup files. Comment out any and all drivers and programs from these two files that are not absolutely essential to running your program under Windows. Change your Windows video driver to the standard Windows VGA driver. Shut down Windows and reboot your machine. This will eliminate the possibility that there is some other program or driver which is interfering with your program.
If you cannot locate a solution and are unable to isolate or resolve the problem with any of these methods, it's time to look for help. See the technical support documentation.
For More Information Breakpoints are described in "Using a Breakpoint to Selectively Halt Execution" earlier in this chapter. Read more about Watch expressions in "Monitoring Data with Watch Expressions." The Immediate window is discussed in "Testing Data and Procedures with the Immediate Window." See "Verifying Your Code with Assertions" for more about the Assert method of the Debug object.