Results 1 to 1 of 1

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

Threaded View

  1. #1

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

    Using VB6 Debug - Descriptions of the Components Part 1

    The following is an explanation of what each button does. (I've changed the order a bit.)

    Start/Continue (toggle)
    Availability: Design time and when in break mode
    Shortcut: F5

    Clicking Start not surprisingly starts your program running just likes F5 does. After Start is clicked it is disabled.

    Break
    Availability: Run time only
    Shortcut: CTRL+BREAK

    Once your program has started, and, for example, the main form has loaded and it is ready for interaction with the user, pressing the Break button pauses the execution of the program and takes you to the edit window for the form. Pressing the button also has the effect of enabling the Start button and toggling it to Continue. At this point you can do things like adding or changing code in the Click event of a command button. Pressing the Continue button takes you back to Run mode where you can now click the command button to see the effect of your change. The breaking/changing code/continuing/testing code cycle can of course be repeated as many times as needed.

    Immediate Window
    Availability: At all times but it is read-only at run time
    Shortcut: CTRL+G

    The Immediate Window is one of three debugging windows that allow you to monitor the values of expressions and variables while stepping through the statements in your application. The other two windows (which I'll discuss later) are the Watch window and the Locals window.

    There are at least four very useful things that you can do with the Immediate Window.

    1) Display the results of Debug.Print statements

    As an example let's assume you have the following code in your command button.
    Code:
    Private Sub Command1_Click()
    
        Dim lngCounter As Long
        
        For lngCounter = 1 To 3
            Debug.Print lngCounter * 20
        Next
        
    End Sub
    When you click the command button, the Debug.Print statements will cause the value of lngCounter * 20 to be printed to the Immediate Window so if you then click the Immediate Window button you will see the following.
    Name:  Immediate Debug.jpg
Views: 29451
Size:  12.0 KB

    2) Check the value of variables

    While you are in break mode you can display the values of the global variables in your form. For example if you have a Private intMyInt As Integer statement you can see what the value of intMyInt is by typing ?intMyInt in the Immediate Window and pressing return. The same thing can be accomplished by typing print intMyInt. The values of such things as command button and label captions, textbox text, etc. can be displayed as well. In addition you can see the value of variables in a Sub or Function if you are at a break point but more about that later.

    3) Change the value of variables and properties

    If you type something like intMyInt = 7 (note that you don't use the question mark) VB will change the value as requested and you can continue execution using that new value. This can be a big time-saver when debugging. A big time-saver when designing a form is that you can, for example, change the value of the Left property of a command button and immediately see what it looks like on the form. If it still needs adjusting you can change it again and again if necessary until you get it right. In this case you will have to record that value in the IDE, but it is much better than having to repetatively change the value, run the program, see how it looks, stop, change the value, run the program, etc. until you get it right.

    4) Run code

    It's nice to be able to display the values of variables in the Immediate Window but if you have a set of variables you need to display and you need to do it more than once, retyping them can be tedious. Fortunately there's a better way. You can simply create a Sub that you run from the Immediate Window! As an example let's assume that you have 3 variables that are involved in your debugging efforts. You would then create a Sub that looks something like this.
    Code:
    Private Sub DisplayDebugValues()
        Debug.Print "The value of intMyInt is " & intMyInt
        Debug.Print "The value of lngCounter is " & lngCounter
        Debug.Print "The value of strSomeThing is " & strSomeThing
    End Sub
    Once you have that Sub you can display them any time you are in break mode by simply typing the name of the Sub in the Immediate Window, pressing Enter, and the three values will be displayed.

    Toggle Breakpoint
    Availability: At design time and when in break mode
    Shortcut: F9

    A breakpoint is a place at which the program will pause until you manually let it resume. To set a breakpoint you can either place the cursor on the line where you want the code to pause and then press F9, or, more simply, just click in the margin of the that line. When you do either of those things VB places a red bullet in the margin and also highlights the line in red. Here is an example where a breakpoint has been placed on a line.
    Name:  Breakpoint.jpg
Views: 29267
Size:  26.4 KB
    If you then run the program and the breakpoint is reached, VB will highlight the line's code in yellow and then wait there for you to do whatever you want. One of the things that you might want to do is to check the value of variables in the Sub or Function (these are called Local variables) and there are several ways to do that while the code in the Sub or Function is paused. You can hover the mouse over a variable and VB will pop up a little box that shows the variable name and its value, or you can bring up the Immediate Window as we discussed previously, or you can highlight any valid expression and click the Quick Watch window or you can click the Locals Window. The latter two will be described later.

    I should mention here that while F9 will toggle a breakpoint on and off, CTRL+SHIFT+F9 will turn all breakpoints off.


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