You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q308469
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

  1. Start Visual Studio .NET or Visual Studio 2005, and create a new Visual Basic Console Application project named Debug.
  2. 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("Good Morning")        Else            MyGreeting = Reverse("Good Afternoon")        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 FunctionEnd Module					
  3. Save the project.
back to the top

Create an unconditional breakpoint

  1. On the Debug menu, click Windows, and then click Breakpoints.
  2. 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					
  3. On the Debug menu, click Start. This starts the program in debug mode. The program stops when it reaches the breakpoint.
  4. On the Debug menu, click Windows, and then click Locals. This displays the values of the local variables, MyGreeting and MyTime.
  5. On the Debug menu, click Continue (or press F5). This continues program execution following the breakpoint.
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

  • 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)					
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

  1. 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.
  2. If you are running this program in the morning, you see the following statement:
    MyGreeting = Reverse("Good Morning")					
    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("Good Afternoon")					
  3. On the Debug menu, click Step Into. This steps into the Reverse function.
  4. 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)					
  5. Remove the current breakpoint by clicking in the left margin.
  6. On the Debug menu, click Step Out. This steps you out of the Reverse function.
  7. Continue until the debugging process is complete.
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

  1. 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 					
  2. In the Breakpoints window, clear the check box for this breakpoint. This disables the breakpoint.
  3. In the Breakpoints window, click Disable All Breakpoints, click Enable All Breakpoints, and then click Clear All Breakpoints.
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

REFERENCES

For more information about debugging breakpoints, see the following topic in the Visual Studio .NET online Help documentation:back to the top
Properties

Article ID: 308469 - Last Review: 03/06/2006 22:20:24 - Revision: 2.3

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

  • kbvs2005applies kbvs2005swept kbhowtomaster KB308469
Feedback
y>