⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<P><HR><strong>TIP:</strong> The fact that conditional breakpoints slow down program execution	can work in your favor at times. If you have a process that you want to view in slow	motion, set one or more conditional breakpoints in that section of code. Set the	conditions so that they will never be met, and your program will slow down but not	stop. <HR></BLOCKQUOTE><H3><A NAME="Heading8"></A>The Run to Cursor Command</H3><P>There is another debugging command that deserves mention here. The Run to Cursorcommand (found on the Run menu on the main menu and on the Code Editor context menu)runs the program until the source line containing the editing cursor is reached.At that point, the program stops as if a breakpoint were placed on that line.</P><P>Run to Cursor acts as a temporary breakpoint. You can use this command ratherthan set a breakpoint on a line that you want to immediately inspect. Just placethe cursor on the line you want to break on and choose Run to Cursor (or press F4).The debugger behaves exactly as if you had placed a breakpoint on that line. Thebenefit is that you don't have to clear the breakpoint after you are done debuggingthat section of code.</P><P><H2>Watching Variables</H2><P>So what do you do when you stop at a breakpoint? Usually you stop at a breakpointto inspect the value of one or more variables. You might want to ensure that a particularvariable has the value you think it should, or you might not have any idea what avariable's value is and simply want to find out.</P><P>The function of the Watch List is basic: It enables you to inspect the valuesof variables. Programmers often overlook this simple but essential feature becausethey don't take the time to learn how to fully use the debugger. You can add as manyvariables to the Watch List as you like. Figure 10.3 shows the Watch List duringa debugging session.</P><P><A HREF="javascript:popUp('28671003.gif')"><B>FIGURE 10.3.</B></A><B> </B><I>TheWatch List in action.</I></P><P>The variable name is displayed in the Watch List followed by its value. How thevariable value is displayed is determined by the variable's data type and the currentdisplay settings for that watch item. I'll discuss the Watch List window in detailin just a bit, but first I want to tell you about a feature that makes inspectingvariables easy.</P><P><H3><A NAME="Heading9"></A>Tooltip Expression Evaluation</H3><P>The debugger and Code Editor have a nice feature that makes checking the valueof a variable easy. This feature, the Tooltip expression evaluator, is on by default,so you don't have to do anything special to use it. If you want, you can turn offthe Tooltip evaluator via the Code Insight page of the Environment Options dialogbox (the Code Insight page was discussed yesterday).</P><P>So what is Tooltip expression evaluation (besides hard to say)? It works likethis: After you stop at a breakpoint, you place the editing cursor over a variableand a tooltip window pops up showing the variable's current value. This makes iteasy to quickly inspect variables. Just place your cursor over a variable and waita half second or so.</P><P>The Tooltip evaluator has different displays for different variable types. Forregular data members (Integer, Char, Byte, string, and so on), the actual value ofthe variable is displayed. For dynamically created objects (an instance of a class,for example), the Tooltip evaluator shows the memory location of the object. Forrecords, the Tooltip evaluator shows all the record elements. Figure 10.4 shows theTooltip expression evaluator inspecting a record's contents.</P><P><A HREF="javascript:popUp('28671004.gif')"><B>FIGURE 10.4.</B></A><B> </B><I>Tooltipsare a great debugger feature.</I></P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Sometimes the Tooltip evaluator acts as if it's not working properly.	If, for example, you place the editing cursor over a variable that is out of scope,	no tooltip appears. The Tooltip evaluator has nothing to show for that particular	variable, so it doesn't display anything.<BR>	</P>	<P>Be aware, also, that variables optimized by the compiler might not show correct	values. Optimization was discussed yesterday and is also discussed later in this	chapter.<BR>	</P>	<P>Another case where the Tooltip evaluator doesn't work is within a with block.	Take this code, for example: 	<PRE>with Point do begin  X := 20;  Y := 50;  Label1.Caption := IntToStr(X);end;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE>	<P>If you were to place the mouse cursor over the variable X, the Tooltip evaluator	would not report the value of X because X belongs to the target of the with statement	(the Point variable). Instead, place the mouse cursor over the Point variable, and	the debugger shows you the value of Point (including the X field). <HR></BLOCKQUOTE><P>The Tooltip expression evaluator is a great feature, so don't forget to use it.</P><P><H3><A NAME="Heading10"></A>The Watch List Context Menu</H3><P>As with every other Delphi window discussed so far, the Watch List has its owncontext menu. (You'd be disappointed if it didn't, right?) Table 10.4 lists the WatchList context menu items and their descriptions.</P><P><H4>TABLE 10.4. THE WATCH LIST CONTEXT MENU.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Item</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Watch</TD>		<TD ALIGN="LEFT">Enables you to edit the watch item with the Watch Properties dialog box.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Add Watch</TD>		<TD ALIGN="LEFT">Adds a new item to the Watch List.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Enable Watch</TD>		<TD ALIGN="LEFT">Enables the watch item.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Disable Watch</TD>		<TD ALIGN="LEFT">Disables the watch item.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Delete Watch</TD>		<TD ALIGN="LEFT">Removes the watch item from the Watch List.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Enable All Watches</TD>		<TD ALIGN="LEFT">Enables all items in the Watch List.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Disable All Watches</TD>		<TD ALIGN="LEFT">Disables all items in the Watch List.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Delete All Watches</TD>		<TD ALIGN="LEFT">Deletes all items in the Watch List.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Stay on Top</TD>		<TD ALIGN="LEFT">Forces the Watch List to the top of all other windows in the IDE.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Break When Changed</TD>		<TD ALIGN="LEFT">When the variable in the watch window changes, the debugger will break. The watch			variable is displayed in red to indicate that Break When Changed is in effect.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Dockable</TD>		<TD ALIGN="LEFT">Determines whether the Watch List window is dockable.</TD>	</TR></TABLE></P><P>Both the Edit Watch and Add Watch context menu items invoke the Watch Propertiesdialog box, so let's look at that next.</P><P><H2><A NAME="Heading11"></A>Using the Watch Properties Dialog Box</H2><P>You use the Watch Properties dialog box when you add or edit a watch. Figure 10.5shows the Watch Properties dialog box as it looks when editing a variable calledBuff.</P><P><A HREF="javascript:popUp('28671005.gif')"><B>FIGURE 10.5.</B></A><B> </B><I>TheWatch Properties dialog box.</I></P><P>The Expression field at the top of the Watch Properties dialog box is where youenter a variable name to edit or add to the watch list. This field is a combo boxthat can be used to select previously used watch items.</P><P>You use the Repeat count field when you are inspecting arrays. For example, let'ssay you have an array of 20 integers. To inspect the first 10 integers in the array,you would enter the first element of the array in the Expression field (Array[0],for example) and then enter 10 in the Repeat Count field. The first 10 elements ofthe array would then be displayed in the Watch List.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you add just the array name to the Watch List, all elements in	the array will be displayed. Use the Repeat Count field when you want to view only	a specific number of array elements. <HR></BLOCKQUOTE><P>The Digits field is used only when inspecting floating-point numbers. Enter thenumber of significant digits you want to see when your floating-point number is displayedin the Watch List. The displayed digits are rounded, not truncated. Another fieldin this dialog box, the Enabled field, determines whether the watch item is currentlyenabled.</P><P>The remainder of the Watch Properties dialog box is composed of various displayoptions. Each data type has a default display type, which is used if you choose theDefault viewing option. The Default viewing option is the default. (Sorry, there'sjust no other way to say it.) Select the other viewing options to view the data inother ways. Figure 10.6 shows the Watch List window with two variables added andwith various viewing options applied. The Buff variable is a character array, andthe I variable is an integer.</P><P><A HREF="javascript:popUp('28671006.gif')"><B>FIGURE 10.6.</B></A><B> </B><I>TheWatch List with various viewing options.</I></P><P>To modify a watch item, click the item in the Watch List and choose Edit Watchfrom the Watch List context menu. You can also double-click a watch item to editit. The Watch Properties dialog box is displayed, and you can edit the watch itemas needed.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> The fastest way to edit a watch item is to double-click its name in	the Watch List. <HR></BLOCKQUOTE><H3><A NAME="Heading12"></A>Enabling and Disabling Watch Items</H3><P>As with breakpoints, individual items in the Watch List can be enabled or disabled.When a watch item is disabled, it is grayed and its value shows &lt;disabled&gt;.</P><P>To disable a watch item, click the item's name in the Watch List and choose DisableWatch from the Watch List context menu. To enable the watch item again, choose EnableWatch from the context menu.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> You might want to disable watch items that you don't currently want	to watch but will need later. Having a number of enabled items in the Watch List	slows down program execution during the debugging process because all the Watch List	variables must be updated each time a code line executes. <HR></BLOCKQUOTE><H3><A NAME="Heading13"></A>Adding Variables to the Watch List</H3><P>You can add variables to the Watch List in several ways. The quickest way is toclick the variable name in the editor window and then select Add Watch at Cursorfrom the Code Editor context menu or press Ctrl+F5. The watch item will be immediatelyadded to the Watch List. You can then edit the watch item to change the display properties,if needed.</P><P>To add a variable to the watch without first locating it in the source file, chooseRun|Add Watch from the main menu. When the Watch Properties dialog box comes up,enter the name of the variable you want to add to the Watch List and click OK.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Although you can add a class instance variable to the Watch List,	the displayed value will not likely be useful. For viewing all the class data members,	you should use the Debug Inspector, which I'll discuss in a minute. <HR></BLOCKQUOTE><H3><A NAME="Heading14"></A>Using the Watch List</H3><P>When a breakpoint is hit, the Watch List displays the current value of any variablesthat have been added to the Watch List. If the Watch List isn't currently open, youcan choose View|Debug Windows|Watches from the main menu to display it.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Dock the Watch List window to the bottom of the Code Editor window	so that it will always be in view when stepping through code. <HR></BLOCKQUOTE><P>Under certain circumstances, a message will be displayed next to the variableinstead of the variable's value. If, for example, a variable is out of scope or notfound, the Watch List displays Undeclared identifier:'X' next to the variable name.If the program isn't running or isn't stopped at a breakpoint, the Watch List displays[process not accessible] for all watch items. A disabled watch item will have &lt;disabled&gt;next to it. Other messages can be displayed depending on the current state of theapplication or the current state of a particular variable.</P><P>As I said yesterday, you might on occasion see Variable `X' inaccessible heredue to optimization in the Watch List. This is one of the minor disadvantages ofhaving an optimizing compiler. If you need to inspect variables that are subjectto optimization, you must turn optimization off. Turn off the Optimization optionon the Compiler page of the Project Options dialog box. Be aware that variables thathave not been initialized (assigned a value) will report random values until theyare initialized.</P><P>The Watch List is a simple but vital tool in debugging applications. To illustratethe use of the Watch List, perform this exercise:</P>

⌨️ 快捷键说明

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