![]() |
|
|
Debugging your Code (checking for errors) Imagine you've just completed a project that you've been working on for a long time. You happily click on the Start button and run your application, however, the program doesn't run as expected. Calculations give wrong results and other options don't work. What would you do? Who you gonna call? Debug Mode, of course! Debug Mode is essentially a mode that takes you through your code step by step. Therefore, you know what exactly happens to each of your variables and procedures. There are many options that VB gives you when debugging your code, however, this lesson will only show you the main features of this awesome and powerful feature in VB. There are three main types of errors that you can run into as a programmer: Syntax Errors, Run-Time Errors and Logic Errors. Syntax Errors happen when you mistype a command, for example, or write an incomplete structure. VB detects some of these Syntax Errors as you write the code. Run-Time Errors occur when VB tries carrying out an operation that is impossible, like dividing by zero or calling a non-existent file. The final type, Logical Errors, is a completely different story. Everything is compiled properly, with no messages from VB, however, the code doesn't work as expected. This is because of flaws in the "logic" used in coding. The only way to fix this type of error is by testing and analyzing your code. Breakpoints: This is something that you'll want to use a lot. Breakpoints are markers placed on lines in your code that cause VB to pause as soon as it reaches lines with breakpoints. This gives you the option to look inside your code and check everything while the application is still active (To check values of variables, hover your mouse cursor over the desired variable). To set a breakpoint, make sure you're in code view, click on the line where you want VB to break and press F9. To remove it press F9 again. The color should automatically change to red. When you run your program and VB breaks on that line, use the Debug toolbar to step into, out of or over lines of code. Many other options are also available on the Debug toolbar:
Quick Watch will be discussed here, the other options on the Debug toolbar will not be discussed, however, you may find that you'll get used to how they work by practice. Quick Watch allows you to check the value of a property, variable or expression during Debug Mode. To add a Quick Watch to an expression, select it while in Code View and click on the Quick Watch button on the Debug Toolbar. A dialogue will appear showing you the value of the expression. If you want to add the expression to the Watch window, then click on the Add button. |