» Home
» Tutorials
» Search  
» Contact us 

Home » Tutorials » Introduction to Visual Basic .NET » Chapter 9

Chapter 9: Debugging and Error Handling
9.2: Debugging
9.2.3: Debugging with the Locals Window

Debugging with the Locals Window

The Locals window is similar to the Watch window, except that it shows all variables and objects for the current function or procedure. The Locals window also lets you change the value of a variable or object, and the same rules that apply to the Watch window apply here (that is, the program must be paused before a value can be changed). The text for a value that has just changed also turns red, making it easy to spot the variable or object that has just changed.

The Locals window is great if you want a quick glance at everything that is going on in a function or procedure, but it is not very useful for watching the values of one or two variables or expressions. The reason for this is that the Locals window contains all variables and objects in a procedure or function. Therefore, if you have a lot of variables and objects, you will have to scroll through the window constantly to view the various variables and objects. This is where the Watch window comes in handy; it lets

you watch just the variables that you need. In this Exercise, you examine the contents of the Locals window in two different procedures. This will demonstrate how the contents of the Locals window change from one procedure to the next.

 

Exercise - Using the Locals Window

1. To prepare for this exercise, you need to have the current breakpoint set and set a new breakpoint in the ListCustomer procedure. Locate the ListCustomer procedure and set a breakpoint on the one line of code in that procedure:

lstData.Items.Add(customerToList.CustomerID & _
    “ - ” & customerToList.CustomerName)

2. Now start your program.

3. If you do not see the Locals window at the bottom of the IDE, select Debug *Windows * Locals. Notice that at this point the Locals window contains no variables or objects. This is because you have not entered a procedure or function. Click the Start button on the form, and your breakpoint in the ListCustomer procedure will be encountered first and execution will be paused.

4. Notice the various objects and their types listed in the Locals window. The first item in the list is Me, which is the form itself. If you expand this item, you will see all the objects and controls associated with your form. If you expand the customerToList object, you’ll see the properties and variables defined in the Customer class from which this object is derived as shown:

5. Now click the Continue icon on the Debug toolbar until you encounter your second breakpoint.

6. Now take a look at the Locals window, and you will see a different set of objects and variables. The one constant item in both procedures is Me, which is associated with the form.

7. If you step through a couple of lines of code in the loop where the breakpoint has paused your program, you will see the values in the Locals window change, and when a value changes it turns red. This is the same behavior that you saw in the Watch window. You can continue to step through your code, or you can click the Continue icon on the Debug toolbar to let your program run to completion.

8. To clear all breakpoints in your code, you can delete each breakpoint in the Breakpoints window, or you can click the Debug menu and choose Delete All Breakpoints. Once you are done, stop your project.

 

click next Page - Error Handling.

Home | Link to Us | Partner Links | About us | Contact us

Copyright © 2009-2012 F1tutorials.com | All Rights Reserved