How to set conditional breakpoints in Visual Studio .NET or in Visual Studio 2005 (308469)
The information in this article 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 SUMMARYMicrosoft
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("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
- 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.
- On the Debug menu, click Windows, and then click Locals. This displays the values of the local variables, MyGreeting and MyTime.
- 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- In the Breakpoints window, click New to create a new breakpoint.
- On the Function tab, type Reverse for Function. Type 1 for Line, type 1 for Character, and then set Language to Basic.
- 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.
- In the New Breakpoint dialog box, click OK.
- On the Debug menu, click Start.
- The program stops at the IF statement in the Main method. To continue program execution, click Continue on the Debug menu.
- The program stops again at the Reverse function. Continue to run the program.
back to the top
Create an additional breakpoint
back to the top
Change breakpoint properties- To change properties for the new breakpoint, right-click the line of code in the previous step, and then click Breakpoint Properties.
- Click Hit Count.
- To set When the breakpoint is hit at a multiple of 2, type 2 in the text field, and then click OK.
- In the Breakpoint Properties dialog box, click OK.
- On the Debug menu, click Start (or press F5). The program stops at the first breakpoint.
- 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.
- 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")
- On the Debug menu, click Step Into. This steps into the Reverse function.
- 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)
- Remove the current breakpoint by clicking in the left margin.
- On the Debug menu, click Step Out. This steps you out of the Reverse function.
- Continue until the debugging process is complete.
back to the top
Change the Breakpoints window output- In the Breakpoints window, click the Columns menu to display available columns.
- 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
- In the Breakpoints window, clear the check box for this breakpoint. This disables the breakpoint.
- 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
Modification Type: | Major | Last Reviewed: | 3/6/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbHOWTOmaster KB308469 kbAudDeveloper |
---|
|