vel24.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 934 行 · 第 1/2 页

HTM
934
字号
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel24.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp12.htm"><LINK REL="Previous" HREF="vel23.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel24.htm</H2><P ALIGN=LEFT><A HREF="vel23.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velp12.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E179" >What You'll Learn</A><LI><A HREF="#E68E180" >The Debugging Routine</A><LI><A HREF="#E68E181" >The Debugger</A><LI><A HREF="#E68E182" >Entering Break Mode</A><LI><A HREF="#E68E183" >Using the Immediate Window</A><LI><A HREF="#E68E184" >Homework</A><UL><LI><A HREF="#E69E167" >General Knowledge</A><LI><A HREF="#E69E168" >Find the Bug</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E35"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 12, Unit 24</B></FONT></CENTER></H1><BR><A NAME="E67E38"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Debugging For Perfection</B></FONT></CENTER></H2><BR><BR><A NAME="E68E179"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>The debugging routine<BR><BR><LI>The debugger<BR><BR><LI>Entering break mode<BR><BR><LI>Using the Immediate window<BR><BR></UL><P>This unit finishes the book by taking a break from the usual teaching of controls and code. As you write more and more programs, you'll have more and more success at improving your Visual Basic skills. The problem is that your program errors will increase as well. No matter how careful, almost every programmer introduces errors along with working code. Those errors, known as <I>bugs</I>, are sometimes extremely difficult to find.<BR><P>Visual Basic contains several debugging tools that help you hunt, find, and correct the bugs that will appear. Visual Basic actually makes finding bugs fun. Well, perhaps you won't use the term <I>fun</I>, but the debugging tools that Visual Basic provides certainly makes locating those errors easier than finding bugs from programming languages that preceded Visual Basic.<BR><BR><A NAME="E68E180"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Debugging Routine</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Several kinds of errors can occur. Some errors are easy to find and others are not so easy to find. Visual Basic's debugging tools help you zero in on some of the more difficult kinds of program errors that can creep into your code.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>debugger</I> is the integrated tool that helps you find program bugs.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>As described in <A HREF="vel01.htm">Unit 1</A>, there are two general categories of errors that you'll often run across as you use Visual Basic. The debugger can help you locate and fix these errors. You will find both <I>syntax errors</I> and <I>logic errors</I>. <BR><P>A syntax error is the easiest kind of error to find. A syntax error often occurs because you misspell a command, function name, or method. A syntax error can also appear because you rearrange the grammar of a loop such as a For loop with a Step option that appears before the loop's To keyword.<BR><P>As you write your program, Visual Basic will spot syntax errors for you. If you set the Options Environment Syntax Checking option to Yes, Visual Basic actually checks for syntax errors every time you press Enter at the end of any code line that contains an error. Figure 24.1 shows how Visual Basic displays a message box that warns the user of the error.<BR><P><B> <A HREF="24vel01.gif">Figure 24.1. The programmer just typed a syntax error and needs to fix the </B><B>problem.</A></B><BR><P>When you run across a syntax error message box, press Enter to get rid of the message box and fix the problem. Usually, you'll immediately see the misspelled word and correct the error. If you can't locate the error right away, or if you want to come back to the problem later, Visual Basic will allow you to continue writing the rest of the program without displaying the error message again unless you change the same line and fail to correct the problem.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>As you can see from Figure 24.1, Visual Basic doesn't always call the error a <I>syntax error</I> but you'll know from the context of the error message that the statement's syntax (grammar or spelling) is wrong.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If you set the Options Environment Syntax Checking option to No, Visual Basic will ignore all syntax errors until you run the program. At runtime, Visual Basic will display the syntax error message boxes as Visual Basic runs across them.<BR><P>The second kind of error you'll run across is a logic error. Logic errors are more difficult to find. Logic errors aren't errors of grammar or spelling, but are errors in action.<BR><P>Logic errors appear when your program seems to understand everything you typed but produces incorrect results. For example, suppose that you wrote a program that printed paychecks and, every so often, the program made a calculation error and doubled the amount of withholding for all employees who had been with the company more than ten years. If you were running the program and printing the checks, you would probably not notice the errors. You <I>would</I> notice the errors, however, when the employees began pounding on your office door.<BR><P>Sometimes, logic errors don't appear until people begin using the program. You'll get the syntax errors out of a program right away because Visual Basic helps you find them with message boxes. People are the hunters and gatherers of logic errors. When you realize that one of your programs has a problem with a calculation, loop, or a procedure, you must return to the Code window and find the problem. In large programs, those logic errors can be difficult to trace.<BR><P>Logic errors can occur because you forgot to initialize a variable properly, you didn't loop through enough table or list control values, or you reversed data types when you defined variables. Although logic errors are often hard to locate, Visual Basic's integrated debugger, described in the rest of this unit, helps you find logic errors.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>There are two general categories of errors: syntax errors and logic errors. Visual Basic finds all syntax errors in your program. The logic errors require work on your part. The debugger, described next, helps you hunt down offending logic errors.<BR><BR><A NAME="E68E181"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Debugger</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Debug menu produces a list of all of Visual Basic's debugging commands. Using the debugging commands, you'll be able to stop a program before the execution of any line of code, look at variable and control values, and single step through the code starting at any line.<BR><P>Figure 24.2 shows the Debug menu. Visual Basic's debugging tools are extremely powerful and allow you to execute programs using one of several methods that enable you to analyze various parts of the code.<BR><P><B> <A HREF="24vel02.gif">Figure 24.2. The debugging tools are available from the Debug command on the </B><B>menu bar.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>Visual Basic enters the <I>break mode</I> when you halt a program during execution.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>These menu options are all available during the break mode. These are the three modes that a Visual Basic program can be in:<BR><UL><LI>Design mode<BR><BR><LI>Runtime mode<BR><BR><LI>Break mode<BR><BR></UL><P>Visual Basic tells you which mode is current by displaying the words design, run, or break in Visual Basic's title bar at the top of your Visual Basic screen. When you develop the program, the program is in the design mode; when you or the user runs a program, the program is in runtime mode; and when you halt a program to use the debugger to analyze program problems, the program enters the break mode.<BR><P>This unit concerns itself with the break mode. While in break mode, your program retains all variable and control values. Therefore, you can halt the program at any time and look at data values from any line of the code. By comparing the values with what you expect the values to contain, you can find where problems are taking place.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Of Visual Basic's three modes, you'll be using the break mode to debug programs. When a program enters the break mode, the program halts at any line in the program's execution but retains all variable and control values assigned to that point.<BR><BR><A NAME="E68E182"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Entering Break Mode</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>There are several ways to enter the break mode. The most common way is to set a breakpoint. You also can halt a program during the program's execution by intercepting the program's execution with Ctrl+Break or by selecting Break from the Run menu at runtime.<BR><P>You'll always enter the break mode from the runtime mode. Only after you begin a program's execution will the break mode be available, because only at runtime are the variables and controls initialized with values. Here are the ways that you can move from the runtime mode to the break mode:<BR><UL><LI>Press Ctrl+Break during the program's execution at the place where you want to enter the break mode. Stopping on one particular line of code is virtually impossible when using Ctrl+Break.<BR><BR><LI>Select Run Break from the menu bar.<BR><BR><LI>Click the Break toolbar button (the button with the hand icon).<BR><BR><LI>In design mode or during a break mode, set a <I>breakpoint</I> on a particular line at which you want the execution to halt. By setting a breakpoint, you can specify the exact line of code where Visual Basic is to enter the break mode.<BR><BR><LI>The debug menu's Add Watch dialog box enables you to specify a <I>break expression</I> that Visual Basic monitors and uses to halt the program's execution when the expression becomes true.<BR><BR><LI>If a runtime error occurs, such as a division by zero that is an undefined math operation, Visual Basic enters the break mode on the offending line. Sometimes, this kind of error is called a <I>runtime</I> error and fits within the logic error category.<BR><BR></UL><P>The most accurate and common way to enter the break mode is by setting a breakpoint. To set a breakpoint, find the line where you want execution to halt at a breakpoint, and set a breakpoint at that particular line of code. The following steps walk you through setting a breakpoint:<BR><OL><LI>Load the CHECK2.MAK project that comes with this book.<BR><BR><LI>Press F7 to open the Code window. Visual Basic opens the code to the Form_Load() event procedure.<BR><BR><LI>Find the following line of code in Form_Load():<BR>lblPoem = lblPoem &amp; &quot;If you ever hear differently,&quot;<BR><BR><LI>Move the mouse cursor to the line and click the mouse button. The text cursor appears at the mouse click's location.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?