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

Article translations Article translations
Article ID: 308469 - View products that this article applies to.
This article was previously published under Q308469
Expand all | Collapse all

On This Page

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.

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 Function
    
    End Module
    					
  3. Save the project.

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.

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.

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)
    					

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.

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.

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.

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.

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.

REFERENCES

For more information about debugging breakpoints, see the following topic in the Visual Studio .NET online Help documentation:
Debugging Basics: Breakpoints
http://msdn.microsoft.com/en-us/library/4607yxb0(VS.71).aspx

Properties

Article ID: 308469 - Last Review: March 6, 2006 - Revision: 2.3
APPLIES TO
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Visual Studio 2005 Professional Edition
Keywords: 
kbvs2005applies kbvs2005swept kbhowtomaster KB308469

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com