Using VB6 Debug - Descriptions of the Components Part 3-VBForums
Results 1 to 1 of 1

Thread: Using VB6 Debug - Descriptions of the Components Part 3

Threaded View

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    San Jose, CA

    Using VB6 Debug - Descriptions of the Components Part 3

    Watch Window
    Availability: At all times
    Shortcut: (None)

    We now (finally) come to what I consider the heart of debugging; the Watch Window.

    In Part1 I talked about the Quick Watch window which allows you to see the values of variables or valid expressions when you are at a breakpoint. (Note that from here on when I refer to "expression" that I mean both valid expressions and variable.) The weak point of that method is that you have to set the breakpoint before you are able to use the window. A watch on the other hand allows you to set up an expression that VB watches for you as the program runs and optionally pauses the program when a condition you state occurs. In other words you don't have to know where to put the breakpoint - VB does it for you!

    You can create a watch by
    1. Clicking the Watch Window button, right-clicking on it and choosing Add watch... or
    2. Clicking the Debug|Add Watch... menu item or
    3. Highlighting an expression, right-clicking, and choosing Add Watch...
    When you do one of those things you will be presented with the Add watch window which looks like this.
    Name:  Watch Window1.jpg
Views: 4792
Size:  25.3 KB
    Let's now examine the three parts of that window.

    1) Expression
    The Expression is the expression you are interested in. It can be a single variable name like intMyInt or a simple expression like intMyInt = 5 or a complex expression like intMyInt > 5 and SomeOtherVar = 37 and bOK = True. Note that capitalization doesn't count and that while there may be a limit to the complexity or length of the expression, I haven't run across either.

    2) Context
    The Context choices defines for VB where you would like it to watch the Expression, and as you see above the default is all procedures in all modules - in other words the whole program. You will notice I'm sure that the Context choices are two drop-down items and, for example, if you click on the Module drop-down, you will see something like the following where all the modules in the program are listed.
    Name:  Watch Window2.jpg
Views: 4695
Size:  28.0 KB
    If you have selected a module then clicking on Procedure will display the procedures contained in that module.

    2) Watch Type
    There are three Watch Types and you can choose one of them.

    Watch Expression
    In order to use Watch Expression effectively you need to have a breakpoint set. Once the watch is created and the breakpoint is reached, clicking on the Watch window button will display something like the following. Note that the Watch Window is dock-able and once docked there's no need to click on it in order for its contents to be seen.
    Name:  Watch1.jpg
Views: 4690
Size:  38.8 KB
    If you are interested in watching a value, creating a Watch Expression is better than using Quick Watch because the latter requires you to continually highlight the expression you are interested in and a Watch Expression doesn't.

    The eyeglasses symbol under Expression tells you that it's a Watch Expression, and the little box with the plus sign indicates that txtSquare has more than one property that we can look at. Clicking on the plus sign will expand the result to show you all the properties and their values. Note that if the Watch Expression had been txtSquare(Index).Text instead of just txtSquare(Index) that there would be no plus sign since we were specific about the property we were interested in. The other things displayed in the widow are pretty self-explanatory but I'll mention them for completeness. Value is the value of the expression, Type is the type of item you are looking at, and Context is the scope of the Watch Expression.

    This is probably as good a place as any to mention that once you have a Watch Expression you can change or delete any watch by way of the Debug|Edit Watch menu item.

    Break When value Is True
    Name:  Watch2.jpg
Views: 4664
Size:  19.8 KB
    The above is a second watch that I added by highlighting an expression and choosing (All Procedures) and Form1 as the Context and selecting Break When value Is True (indicated by the page-in-hand symbol) as the watch Type. I chose Form1 as the Context because I knew that mstrLastPlayer was a variable with form-wide scope and I wanted to see where in the form it was being changed. Note that if I had chosen (All Procedures) and (All Modules) as the Context that the Value would have said "<Expression not defined in context>" and the Value would always show as "Empty" since the Context was too broad.

    Break When Value Changes
    As far as I know there are no limit to the number of watches you can have and I could add a third watch with mstrLastPlayer as the Expression and Break When Value Changes selected. That type of watch is indicated in the window by a yellow-triangle-in-hand symbol and I'm sure you have already figured out that that watch would pause the program any time mstrLastPlayer changed value.
    Unfortunately watches only exist as long as the particular project is open and can not be saved. Also a watch that you are no longer interested in can get in your way by, for example, continually pausing the program when one of the "Break" type watches becomes true or its value changes. You can of course get around that by deleting the particular watch or you can simply edit the watch by adding say, an "X" at the front of the expression thereby making it invalid. Once it is invalid VB will ignore it but not delete it. Later on if you like you can remove the "X" which will reactivate it.

    Debug Is A Great Tool
    Over the years I've found that Debug has been an invaluable asset in helping me track down the bugs that slip into my programs, and I hope that this tutorial has given you enough of an understanding of it so that you will feel comfortable using it to track down your errors.

    To give you a start at using it I've attached a version of my Tic-Tac-Toe game that has a bug. What happens is that the second time either "X" or "O" wins a game their total shows as 3 rather than 2. You may well be able to find the bug by just visually inspecting the program but I encourage you to try to find it using the tools provided by Debug.

    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width