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

📄 ch10.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<DL>	<DT></DT>	<DD><B>1. </B>Create a new application and place a button on the form. Change the	button's Name property to WatchBtn and its Caption to Watch Test. Change the form's	Name property to DebugMain and the Caption property to whatever you like.	<P>	<DT></DT>	<DD><B>2. </B>Double-click the button to display its OnClick handler in the Code	Editor. Modify the OnClick handler so that it looks like this:	<P></DL><BLOCKQUOTE>	<PRE>procedure TForm1.Button1Click(Sender: TObject);var  S : string;  X, Y : Integer;begin  X := Width;  S := IntToStr(X);  Y := Height;  X := X * Y;</PRE>	<P><B>S := IntToStr(X);</B></P>	<P><B></B></P>	<PRE>  X := X div Y;  S := IntToStr(X);  Width := X;  Height := Y;end;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>3. </B>Save the project. Name the unit DbgMain and the project DebugTst.	<P>	<DT></DT>	<DD><B>4. </B>Set a breakpoint on the first line after the begin statement in the	OnClick handler. Run the program.	<P>	<DT></DT>	<DD><B>5. </B>Click the Watch Test button. The debugger will stop at the breakpoint.	When the debugger stops at a breakpoint, the IDE and Code Editor come to the top.	<P>	<DT></DT>	<DD><B>6. </B>Add watches for the variables S, X, and Y. (Initially the variables	X and Y will be inaccessible due to optimization, but don't worry about that.)	<P>	<DT></DT>	<DD><B>7. </B>Arrange the Watch List and Code Editor so that you can see both (dock	the Watch List to the bottom of the Code Editor if you want).	<P>	<DT></DT>	<DD><B>8. </B>Switch focus to the Code Editor and press F8 to execute the next line	of code. That line is executed and the execution point moves to the next line. The	variable X now shows a value.	<P>	<DT></DT>	<DD><B>9. </B>Continue to step through the program by pressing F8. Watch the results	of the variables in the Watch List.	<P>	<DT></DT>	<DD><B>10. </B>When the execution point gets to the last line in the method, click	the Run button on the toolbar to continue running the program.	<P></DL><P>Click the Watch Test button as many times as you want to get a feel for how theWatch List works. Experiment with different watch settings each time through.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The code in this example obtains the values for the form's Width	and Height properties, performs some calculations, and then sets the Width and Height	back to where they were when you started. In the end nothing changes, but there is	a good reason for assigning values to the Width and Height properties at the end	of the method.<BR>	</P>	<P>If you don't actually do something with the variables X and Y, you can't inspect	them because the compiler will optimize them and they won't be available to watch.	Essentially, the compiler can look ahead, see that the variables are never used,	and just discard them. Putting the variables to use at the end of the method avoids	having them optimized away by the compiler.<BR>	</P>	<P>I've brought this up several times now, but I want to make sure you have a basic	understanding of how an optimizing compiler works. When you start debugging your	applications, this knowledge will help avoid some frustration when you start getting	those Variable `X' inaccessible here due to optimization messages in the Watch List.	<HR></BLOCKQUOTE><H2><A NAME="Heading15"></A>The Debug Inspector</H2><P>The Debug Inspector is a new feature in Delphi 4. Simply stated, the Debug Inspectorenables you to view data objects such as classes and records. You can also inspectsimple data types such as integers, character arrays, and so on, but those are bestviewed with the Watch List. The Debug Inspector is most useful in examining classesand records.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> You can use the Debug Inspector only when program execution is paused	under the debugger. <HR></BLOCKQUOTE><P>To inspect an object, click the object's name in a source file and choose Inspectfrom the Code Editor context menu (or press Alt+F5). You can also choose Run|Inspectfrom the main menu.</P><P>The Debug Inspector window contains details of the object displayed. If the objectis a simple data type, the Debug Inspector window shows the current value (in bothdecimal and hex for numeric data types) and the status line at the bottom displaysthe data type. For example, if you inspect an integer variable, the value will beshown and the status bar will say Integer. At the top of the Debug Inspector is acombo box that initially contains a description of the object being inspected.</P><P>If you are inspecting a class, the Debug Inspector will look like Figure 10.7.</P><P><A HREF="javascript:popUp('28671007.gif')"><B>FIGURE 10.7.</B></A><B> </B><I>TheDebug Inspector inspecting a form class.</I></P><P>To better understand the Debug Inspector, follow these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Load the DebugTst program you created earlier (if it's not already	loaded).	<P>	<DT></DT>	<DD><B>2. </B>Set a breakpoint somewhere in the WatchBtnClick method.	<P>	<DT></DT>	<DD><B>3. </B>Run the program and click the Watch Test button. The debugger stops	at the breakpoint you have set.	<P>	<DT></DT>	<DD><B>4. </B>From the main menu, choose Run|Inspect. The Inspect dialog box is displayed.	<P>	<DT></DT>	<DD><B>5. </B>Type Self in the Expression field and click OK.	<P>	<DT></DT>	<DD><B>6. </B>The Debug Inspector is displayed and you can examine the main form's	data.	<P></DL><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> You can inspect Self only from within a method of a class. If you	happen to set a breakpoint in a regular function or procedure and then attempt to	inspect Self, you will get an error message stating that Self is an invalid symbol.	In the previous example, Self refers to the application's main form. <HR></BLOCKQUOTE><H3><A NAME="Heading16"></A>Debug Inspector Pages</H3><P>When inspecting classes, the Debug Inspector window contains three pages, as youcan see. The first items listed are the data items that belong to the ancestor class.At the end of the list are the items that belong to the immediate class. You canchoose whether to view the ancestor class information. To turn off the ancestor classitems, right-click and select Show Inherited from the Debug Inspector context menu.</P><P>By using the arrow keys to move up and down the data members list, you can tellat a glance what each data member's type is (look at the status bar on the DebugInspector window). To further inspect a data member, double-click the value columnon the line showing the data member. A second Debug Inspector window is opened withthe selected data member displayed. You can have multiple Debug Inspector windowsopen simultaneously.</P><P>The Methods page of the Debug Inspector displays the class's methods. In somecases the Methods tab isn't displayed (when inspecting simple data types, for example).The status bar shows the selected method's declaration.</P><P>The Properties page of the Debug Inspector shows the properties for the classbeing inspected. Viewing the properties of a class is of limited value (the informationpresented is not particularly useful). Most of the time you can accomplish what youare after by inspecting the data member associated with a particular property onthe Data page.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The Methods page and the Properties page of the Debug Inspector are	available only when you're inspecting a class. When you're inspecting simple data	types, the Data page alone is displayed. <HR></P>	<P><HR><strong>TIP:</strong> If you want your Debug Inspector windows always on top of the Code	Editor, go to the Debugger page of the Environment Options dialog box and check the	Inspectors stay on top check box. <HR></BLOCKQUOTE><H3><A NAME="Heading17"></A>Debug Inspector Context Menus</H3><P>The Debug Inspector context menu has several items that enable you to work withthe Debug Inspector and the individual variables. For example, rather than open anew Debug Inspector window for each object, you can right-click and choose Descendto replace the current object in the Debug Inspector window with the selected object.For example, if you are inspecting a form with a button called Button1, you can selectButton1 in the Debug Inspector and choose Descend from the context menu. The DebugInspector will then be inspecting Button1. This method has an added advantage: TheIDE keeps a history list of the objects you inspect. To go back to an object youhave already inspected, just choose the object from the combo box at the top of theDebug Inspector window. Choosing one of the objects in the history list will againshow that object in the Debug Inspector window.</P><P>The Change item on the Debug Inspector context menu enables you to change a variable'svalue.</P><BLOCKQUOTE>	<P><HR><strong>CAUTION:</strong> Take great care when changing variables with the Debug Inspector.	Changing the wrong data member or specifying a value that is invalid for that data	member might cause your program to crash.<HR><B></B></P></BLOCKQUOTE><P>The Inspect item on the Debug Inspector's context menu enables you to open a secondDebug Inspector window with the item under the cursor displayed. The New Expressioncontext menu item enables you to enter a new expression to inspect in the Debug Inspector.</P><P>The Show Inherited item on the Debug Inspector context menu is a toggle that determineshow much information the Debug Inspector should display. When the Show Inheritedoption is on, the Debug Inspector shows all data members, methods, and propertiesof the class being inspected as well as the data members, methods, and propertiesof the immediate ancestor class. When the Show Inherited option is off, only thedata members, methods, and properties of the class itself are shown. Turning offthis option can speed up the Debug Inspector because it doesn't have as much informationto display.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> If you have a class data member and you don't remember its type, you	can click on it when you are stopped at a breakpoint and press Alt+F5 to display	the Debug Inspector. The status bar at the bottom of the Debug Inspector window will	tell you the variable's data type. <HR></BLOCKQUOTE><H2><A NAME="Heading18"></A>Other Debugging Tools</H2><P>Delphi has some additional debugging tools to aid you in tracking down bugs. Someof these tools are, by nature, advanced debugging tools. Although the advanced debuggingtools are not as commonly used as the other tools, they are very powerful in thehands of an experienced programmer.</P><P><H3><A NAME="Heading19"></A>The Evaluate/Modify Dialog Box</H3><P>The Evaluate/Modify dialog box enables you to inspect a variable's current valueand to modify the value if you want. Using this dialog box, you can test for differentoutcomes by modifying a particular variable. This enables you to play a what-if gamewith your program as it runs. Changing the value of a variable while debugging allowsyou to test the effects of different parameters of your program without recompilingeach time. Figure 10.8 shows the Evaluate/Modify dialog box inspecting a variable.</P><P><A HREF="javascript:popUp('28671008.gif')"><B>FIGURE 10.8.</B></A>  <I>The Evaluate/Modifydialog box.</I></P><P></P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The Evaluate/Modify dialog box's toolbar can display either large	toolbar buttons or small toolbar buttons. By default, it shows small toolbar buttons.	The small buttons don't have captions, so you will have to pass your mouse cursor	over the buttons and read the tooltip to see what each button does. To see the large	toolbar buttons, drag the sizing bar immediately below the toolbar downward to resize	the toolbar. The toolbar will then show the large toolbar buttons with captions underneath	each button. Figure 10.8 shows the Evaluate/Modify dialog box with large toolbar

⌨️ 快捷键说明

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