Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать
window automatically. As you step through the code, the value of the variable is updated automatically in the Autos window.
Before using the Autos window, edit the OnMenuMymenu1 function and add the following code:
int i;

290 Project 1 DATABASE PROGRAMMING USING VC++.NET

for(i=0; i<3;i++)

 

 

Y

 

AfxMessageBox(“hi”);

 

 

int j;

 

 

 

j=i;

 

 

 

 

AfxMessageBox(“The End”);

 

 

 

 

 

 

 

Next, perform the following steps to learnLto use the Autos window:

 

 

 

 

F

 

1. Insert a breakpoint next to the for loop.

 

2. Build and execute your application. Choose Menu, MyMenu1. The

 

 

 

 

M

 

 

 

application enters break mode. The value of the variable i appears in the

 

 

 

A

 

 

 

Autos window, shown in Figure 9-16. The variable j is not displayed in

 

 

the Autos windowEbecause it is not located in the currently executed

 

 

statement or in the previously executed statement.

 

 

T

 

 

FIGURE 9-16 The Autos window

3.Press F10 to step over the code. This will help you to examine the execution line by line and track the value of the variables in the Autos window. After every message box, note that the value of the variable i changes in the Autos window. Once the loop is executed three times, the value of i is assigned to j. Then, the value of j also appears in the Autos window.

4.After you receive the last message box, with the message The End, press F5 to continue the execution.

5.End the application.

Team-Fly®

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

291

 

 

 

 

 

Locals Window

The Locals window displays all local variables, which are variables within the current function that is executed. Perform the following steps to learn how to use the Locals window:

1.Execute the application and choose Menu, MyMenu1.

2.In break mode, activate the Locals window. Unlike the Autos window, the Locals window displays the values of both variables i and j, as shown in Figure 9-17.

FIGURE 9-17 The Locals window

3.Press F10 and see the values changing in the Locals window.

4.After you receive the last message box with the message The End, press F5 to continue the execution.

5.End the application.

Watch Window

You use the Autos and Locals windows to monitor variables that are added automatically by the debugger, whereas you use the Watch window to add any variables and expressions that you want to evaluate and monitor. You can change the values of variables only, not constants. In addition to variables and expressions, you can also monitor register values, but for native code only.

The debugger allows you to add four Watch windows (Watch 1 to Watch 4), if required, to your application. By default, only the Watch 1 window is visible. To add more windows, in break mode, choose Debug, Windows, Watch, Watch <n>, where <n> takes the value from 1 to 4.

To add a variable or an expression to the Watch window:

1.Execute your application and choose Menu, MyMenu1. The application enters break mode.

292Project 1 DATABASE PROGRAMMING USING VC++.NET

2.In break mode, press F10 and then activate the Watch 1 window.

3.Under the Name column, type “i” and press Enter. The value of the variable added appears in the corresponding Value column. The data type of the variable is displayed in the Type column.

4.In the next Name row, type “i+2” and press Enter. The expression is evaluated and the result displayed in the corresponding Value column. Figure 9-18 displays the Watch 1 window with the variable added.

FIGURE 9-18 The Watch 1 window

5.Choose Debug, Windows, Watch, Watch 2 to add the second Watch window.

6.In the Watch 2 window, add the expression “j=”.

7.Press F10 and execute the application. Notice the change in the value of i in the Watch 1 window and the corresponding change in the value of j in the Watch 2 window.

8.End the application.

QuickWatch Dialog Box

The QuickWatch dialog box, as the name indicates, is a modal dialog box that enables you to quickly evaluate a variable or expression. Figure 9-19 displays the QuickWatch dialog box.

The variables or expressions that you add to the QuickWatch dialog box can be either recalculated or added to the Watch 1 window. Because the QuickWatch dialog box is a modal dialog box, if you want to step through the code, you need to add the variables and expressions first to the Watch 1 window, close the dialog box, and then proceed.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

293

 

 

 

 

 

FIGURE 9-19 The QuickWatch dialog box

Other Debugging Windows

So far, you learned to work with some of the debugging windows and tools provided by the debugger. The following list briefly describes the other debugging windows:

This window. The ‘This’ window enables you to monitor all member data associated with the object that invoked the active method. To invoke the This window, choose Debug, Windows, This.

Call Stack window. This window specifies the sequence in which the functions in your application have executed before entering break mode. It displays the name of the function along with the parameter types and values and the language used to create the function. The Call Stack window is by default displayed in break mode. If not, choose Debug, Windows, Call Stack to view the window.

Command/Immediate window. This window works in two modes: Command and Immediate. Command mode is used to execute commands directly from within the window. Immediate mode is used to evaluate expressions and statements. For example, in break mode, if you type “?i” in the Command window and press Enter, Immediate mode is activated and the result is displayed.

294Project 1 DATABASE PROGRAMMING USING VC++.NET

Threads window. This window helps you to monitor various threads in a multithreaded application. It allows you to monitor and manipulate the threads. Choose Debug, Windows, Threads to view the Threads window.

Module window. This window lists the modules used in your application. Modules are the DLLs and EXEs included in your application. Choose Debug, Windows, Modules to view the Modules window.

Memory window. This window allows you to view large data, such as buffers, that cannot be easily monitored using other windows. Similar to Watch windows, you can add four Memory windows to monitor your application. To add a Memory window, choose Debug, Windows, Memory, Memory 1.

Registers window. This window displays the contents of a register. To view this window, choose Debug, Windows, Registers.

Disassembly window. This window allows you to examine the assembly code generated by the compiler against the source code of your application. To view this window, choose Debug, Windows, Disassembly.

TIP

Assemblers are programs that translate code in assembly language to its equivalent machine language. Disassemblers do the reverse — they translate the machine code to its equivalent assembly language code.

CAUTION

Except for the Breakpoints and Immediate windows, you can work with all other windows only in break mode.

Summary

This chapter introduced you to the basics of debugging and exception handling. You learned how to handle exceptions in an MFC application. You also learned to use some of the predominantly used debugging windows.

DEBUGGING AND EXCEPTION HANDLING IN VC++.NET

Chapter 9

 

295

 

 

 

 

 

Up to this point, you have been learning about MFC that is same as it was in Visual C++ 6.0. From this point forward, the chapters deal with creating managed applications, thereby introducing you to the real Visual C++ .NET. Welcome aboard Visual C++ .NET!

This page intentionally left blank

PARTIIIProfessional

Project – 2

This page intentionally left blank

Project 2

Creating an

Application Using

Managed C++

Application