Section C.2. Breakpoints and the Continue Command


C.2. Breakpoints and the Continue Command

We begin by investigating breakpoints, which are markers that can be set at any executable line of code. When a running program reaches a breakpoint, execution pauses, allowing you to examine the values of variables to help determine whether logic errors exist. For example, you can examine the value of a variable that stores the result of a calculation to determine whether the calculation was performed correctly. You can also examine the value of an expression.

To illustrate the debugger features, we use the program in Fig. C.1 and C.2 that creates and manipulates an Account (Fig. C.1) object. This example is based on the concepts presented through Chapter 4, so it does not use features that are presented after that chapter. Execution begins in Main (lines 4-33 of Fig. C.2). Line 5 creates an Account object with an initial balance of $50.00. Account's constructor (lines 8-14 of Fig. C.1) accepts one argument, which specifies the Account's initial balance. Line 8 of Fig. C.2 outputs the initial account balance using Account property Balance. Lines 11-12 prompt the user for and input the withdrawalAmount. Lines 14-16 subtract the withdrawal amount from the Account's balanceValue using its Debit method. Lines 19-20 display the new balanceValue. Next, lines 23-32 perform similar steps to credit the account.

Figure C.1. Account class with a constructor to initialize variable balance

  1  ' Fig. C.01: Account.vb  2  ' Account class with a constructor to initialize a customer's balance.  3  Public Class Account  4     ' instance variable that stores the balance  5     Private balanceValue As Integer  6  7     ' constructor  8     Public Sub New(ByVal initialBalance As Integer)  9        ' if initialBalance is not greater than 0, 10        ' balance is still initialized to 0 by default 11        If initialBalance > 0 Then 12           Balance = initialBalance 13        End If 14     End Sub ' New 15 16     ' credit (increases) the account by amount 17     Public Sub Credit(ByVal amount As Integer) 18        Balance = Balance + amount ' add amount to balance 19     End Sub ' Credit 20 21     ' debits (decreases) the account by amount 22     Public Sub Debit(ByVal amount As Integer) 23        If amount > Balance Then 24           Console.WriteLine("Debit amount exceeded account balance.") 25        End If 26 27        If amount <= Balance Then 28           Balance = Balance - amount ' subtract amount to balance 29        End If 30     End Sub ' Debit 31 32     ' property makes balanceValue available to clients; 33     ' no validation required 34     Public Property Balance() As Integer 35        Get ' returns the account balance 36           Return balanceValue 37        End Get 38 39        Set(ByVal value As Integer) ' sets the balance value 40           balanceValue = value 41        End Set 42     End Property ' Balance 43  End Class ' Account 

Figure C.2. Creating and manipulating an Account object.

  1  ' Fig. C.02 AccountTest.vb  2  ' Create and manipulate an Account object.  3  Module AccountTest  4     Sub Main() ' begins execution  5        Dim account1 As New Account(50) ' create Account object  6  7        ' display initial balance of each object  8        Console.WriteLine("account1 balance: " & account1.Balance)  9 10        ' obtain withdrawal input from Command Prompt 11        Console.Write("Enter withdrawal amount for account1: ") 12        Dim withdrawalAmount As Integer = Console.ReadLine() 13 14        Console.WriteLine(vbCrLf & "Subtracting " & withdrawalAmount & _ 15           " from account1 balance") 16        account1.Debit(withdrawalAmount) ' subtract amount from account1 17 18        ' display balance 19        Console.WriteLine("account1 balance: " & account1.Balance) 20        Console.WriteLine() 21 22        ' obtain credit input from Command Prompt 23        Console.Write("Enter credit amount for account1: ") 24        Dim creditAmount As Integer = Console.ReadLine() 25 26        Console.WriteLine(vbCrLf & "Adding " & creditAmount & _ 27           " to account1 balance") 28        account1.Credit(creditAmount) ' add amount to account1 29 30        ' display balance 31        Console.WriteLine("account1 balance: " & account1.Balance) 32        Console.WriteLine() 33     End Sub ' Main 34  End Module ' AccountTest 

 account1 balance: 50 Enter withdrawal amount for account1: 25 Subtracting 25 from account1 balance account1 balance: 25 Enter credit amount for account1: 33 Adding 33 to account1 balance  account1 balance: 58 



In the following steps, you will use breakpoints and various debugger commands to examine the value of the variable withdrawalAmount (declared in Fig. C.2) while the program executes.

1.

Inserting breakpoints in Visual Studio. First, ensure that AccountTest.vb is open in the IDE's code editor. To insert a breakpoint, left click inside the margin indicator bar (the gray margin at the left of the code window in Fig. C.3) next to the line of code at which you wish to break, or right click that line of code and select Breakpoint > Insert Breakpoint. You can set as many breakpoints as you like. Set breakpoints at lines 11, 16 and 33 of your code. A solid circle appears in the margin indicator bar where you clicked and the entire code statement is highlighted, indicating that breakpoints have been set (Fig. C.3). When the program runs, the debugger suspends execution at any line that contains a breakpoint. The program then enters break mode. Breakpoints can be set before running a program, in break mode and during execution.

Figure C.3. Setting breakpoints.


2.

Beginning the debugging process. After setting breakpoints in the code editor, select Build > Build Account to compile the program, then select Debug > Start Debugging (or press the F5 key) to begin the debugging process. While debugging a console application, the Command Prompt window appears (Fig. C.4), allowing program interaction (input and output).

Figure C.4. Account program running.


3.

Examining program execution. Program execution pauses at the first breakpoint (line 11), and the IDE becomes the active window (Fig. C.5). The yellow arrow to the left of line 11 indicates that this line contains the next statement to execute. The IDE also highlights the line as well.



Figure C.5. Program execution suspended at the first breakpoint.


4.

Using the Continue command to resume execution. To resume execution, select Debug > Continue (or press the F5 key). The Continue command will execute the statements from the current point in the program to the next breakpoint or the end of Main, whichever comes first. The program continues executing and pauses for input at line 12. Enter 25 in the Command Prompt window as the withdrawal amount. When you press Enter, the program executes until it stops at the next breakpoint (line 16). Notice that when you place the mouse pointer over the variable name withdrawalAmount, its value is displayed in a Quick Info box (Fig. C.6). As you'll see, this can help you spot logic errors in your programs.

Figure C.6. QuickInfo box displays value of variable depositAmount.


5.

Continuing program execution. Use the Debug > Continue command to execute line 16. The program then asks for you to input a credit (deposit) amount. Enter 33, then press Enter. The program displays the result of its calculation (Fig. C.7).

Figure C.7. Program output.


6.

Disabling a breakpoint. To disable a breakpoint, right click a line of code in which the breakpoint has been set and select Breakpoint > Disable Breakpoint. The disabled breakpoint is indicated by a hollow circle (Fig. C.8)the breakpoint can be re-enabled by right clicking the line marked by the hollow circle and selecting Breakpoint > Enable Breakpoint.



Figure C.8. Disabled breakpoint.


7.

Removing a breakpoint. To remove a breakpoint that you no longer need, right click the line of code on which the breakpoint has been set and select Breakpoint > Delete Breakpoint. You also can remove a breakpoint by clicking the circle in the margin indicator bar.

8.

Finishing program execution. Select Debug > Continue to execute the program to completion. Then delete all the breakpoints.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net