Microsoft KB Archive/308469

= How to set conditional breakpoints in Visual Studio .NET or in Visual Studio 2005 =

Article ID: 308469

Article Last Modified on 3/6/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio 2005 Professional Edition

-



This article was previously published under Q308469



In This Task

 * SUMMARY
 * Create sample code
 * Create an unconditional breakpoint
 * Create a conditional function breakpoint
 * Create an additional breakpoint
 * Change breakpoint properties
 * Step through code in Debug mode
 * Change the Breakpoints window output
 * Disable and clear breakpoints
 * Troubleshooting
 * REFERENCES



SUMMARY
Microsoft Visual Studio .NET and Microsoft Visual Studio 2005 provide several ways to help you debug .NET Framework applications. This step-by-step article explains how to use breakpoints to debug a section of code that is written in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005.

This article assumes that you are familiar with Visual Basic .NET syntax or Visual Basic 2005 syntax.

back to the top

Create sample code
 Start Visual Studio .NET or Visual Studio 2005, and create a new Visual Basic Console Application project named Debug.  Modify the code in Module1.vb as follows: Module Module1

Sub Main

Dim MyTime As String = TimeString Dim MyGreeting As String

' Create a greeting. If CInt(TimeString.Substring(0, 2)) < 12 Then MyGreeting = Reverse(&quot;Good Morning&quot;) Else MyGreeting = Reverse(&quot;Good Afternoon&quot;) End If

' Display reversed greeting. Console.WriteLine(MyGreeting)

End Sub

Function Reverse(ByVal inStr As String) As String

' Reverses the characters contained in a string. Dim MyInt As Integer Dim MyStr As String

For MyInt = (inStr.Length - 1) To 0 Step -1 MyStr = MyStr & inStr.Substring(MyInt, 1) Next

Return MyStr

End Function

End Module  Save the project.

back to the top

Create an unconditional breakpoint
 On the Debug menu, click Windows, and then click Breakpoints.  In the Code view, click in the left margin next to the following line of code. This sets a breakpoint at that statement: If CInt(TimeString.Substring(0, 2)) < 12 Then  On the Debug menu, click Start. This starts the program in debug mode. The program stops when it reaches the breakpoint.</li> On the Debug menu, click Windows, and then click Locals. This displays the values of the local variables, MyGreeting and MyTime.</li> On the Debug menu, click Continue (or press F5). This continues program execution following the breakpoint.</li></ol>

back to the top

Create a conditional function breakpoint

 * 1) In the Breakpoints window, click New to create a new breakpoint.
 * 2) On the Function tab, type Reverse for Function. Type 1 for Line, type 1 for Character, and then set Language to Basic.
 * 3) Click Condition and make sure that the Condition check box is selected. Type instr.length > 0 for Condition, make sure that the is true option is selected, and then click OK.
 * 4) In the New Breakpoint dialog box, click OK.
 * 5) On the Debug menu, click Start.
 * 6) The program stops at the IF statement in the Main method. To continue program execution, click Continue on the Debug menu.
 * 7) The program stops again at the Reverse function. Continue to run the program.

back to the top

Create an additional breakpoint
<ul>  To create an additional breakpoint, click in the left margin of the Reverse function next to the following line of code: MyStr = MyStr & inStr.Substring(MyInt, 1) </li></ul>

back to the top

Change breakpoint properties

 * 1) To change properties for the new breakpoint, right-click the line of code in the previous step, and then click Breakpoint Properties.
 * 2) Click Hit Count.
 * 3) To set When the breakpoint is hit at a multiple of 2, type 2 in the text field, and then click OK.
 * 4) In the Breakpoint Properties dialog box, click OK.
 * 5) On the Debug menu, click Start (or press F5). The program stops at the first breakpoint.
 * 6) Continue to run the program until the debugging process is complete.

back to the top

Step through code in Debug mode
 Start the program in debug mode. When the program reaches the first breakpoint, on the Debug menu, click Step Over. This steps over the breakpoint and onto the next statement.</li>  If you are running this program in the morning, you see the following statement: MyGreeting = Reverse(&quot;Good Morning&quot;) If you are running the program in the afternoon, you see the Else statement. Press F10 again to step onto the following statement: MyGreeting = Reverse(&quot;Good Afternoon&quot;) </li> On the Debug menu, click Step Into. This steps into the Reverse function.</li>  Continue to click Step Over on the Debug menu until you reach the following statement for the second time: MyStr = MyStr & inStr.Substring(MyInt, 1) </li> Remove the current breakpoint by clicking in the left margin.</li> On the Debug menu, click Step Out. This steps you out of the Reverse function.</li> Continue until the debugging process is complete.</li></ol>

back to the top

Change the Breakpoints window output

 * 1) In the Breakpoints window, click the Columns menu to display available columns.
 * 2) Click Function to add the Function column to the Breakpoints window display.

back to the top

Disable and clear breakpoints
  In the Breakpoints window, double-click the following breakpoint:

Module1.vb, line 9 character 1

This takes you to the location of the breakpoint in the Code window: If CInt(TimeString.Substring(0, 2)) < 12 Then </li> In the Breakpoints window, clear the check box for this breakpoint. This disables the breakpoint.</li> In the Breakpoints window, click Disable All Breakpoints, click Enable All Breakpoints, and then click Clear All Breakpoints.</li></ol>

back to the top

Troubleshooting

 * You may create breakpoints only on executable lines of code. For example, breakpoints are not acceptable on comments or on variable declarations without default values.
 * You may assign function breakpoints on only the first line of the function declaration.
 * Microsoft does not support data breakpoints in Visual Basic .NET.

back to the top