Reputation: 2424
I am using Visual Studio 2010, and I know this feature is available in C++.
I need to debug some code, that changes a variable to several values. I want to debug the code in a specific case, when the variable getting a specific value. I know I can add if(var == value)
, but is there any elegant way to do it?
Another question, can I set a breakpoint when a variable is changed in general?
Upvotes: 57
Views: 70617
Reputation: 103
Conditional breakpoints are possible as other answers already pointed out. As JaredPar explains you can set a breakpoint, right click on it, select "Conditions" and type your condition(s).
Since Visual Studio 2019 Preview 2 the so called "Data Breakpoints" are availble. While in debugging mode you can select a variable in your "Autos" or "Locals" window and with right click "Break Whan Value Changes" you can archive just that.
This article from Microsoft DevBlogs explains it pretty good: Break When Value Changes: Data Breakpoints for .NET Core in Visual Studio 2019
Upvotes: 0
Reputation: 40096
In VisualStudio Code, you can set conditional breakpoints as follows:
Click in gutter to create a red-dot breakpoint
Choose Debug from left-side toolbar (icon: circle-slash over bug)
There are four sections: Variables, Watch, Call Stack and Breakpoints
Expand Breakpoints section so you can see the breakpoints
Right-Click on the desired breakpoint
Choose Edit Breakpoint...
Set your condition and press <Enter>. For example:
myvar == 1234
or
'stophere' in myvar
etc
References:
https://code.visualstudio.com/docs/editor/debugging#_conditional-breakpoints
Upvotes: 1
Reputation: 755317
It is certainly possible to set a condition like a variable receiving a certain value. This is known as a breakpoint condition. To create one, do the following.
Now the breakpoint will only hit when your conditional evaluates to true.
The second item you asked for, breaking when a variable's value changes for any reason, is known as a data breakpoint. These are only available for C++ code. It's not an option in C#, VB.NET or any other managed language.
Upvotes: 109
Reputation: 1436
You can use conditional breakpoints. I know your question was specific to VS2010, but be aware that from VS2012 on, you have to switch to the Managed Compatibility Mode, to use conditional breakpoints in Visual Basic. Why and how is described here:
switching-to-managed-compatibility-mode-in-visual-studio-2013
Upvotes: 2
Reputation: 2996
It depends on the scope of your breakpoint. If the variable is not local or not static you won't be able to.
To set the condition of a breakpoint, right click it and you should get this screen:
Pick Condition...
Upvotes: 3
Reputation: 54127
So long as you are using a Visual Studio edition other than Express, you can achieve this in C# using a breakpoint condition.
In the Breakpoint Condition dialog box, enter a valid expression in the Condition box, such as myLocalVariable > 1
and
...choose Has changed if you want to break when the value of the expression has changed.
To get to the Has changed option, right-click your breakpoint in the Breakpoints window and select Condition..., then check the screenshot below.
Upvotes: 6
Reputation: 54630
You can do both of these things.
var==value
and select "Is True".Upvotes: 0
Reputation: 160992
Add a breakpoint with F9 - right click it and select "Condition..."
- now you can add a boolean condition and the breakpoint will only get hit if that condition evaluates to true.
Upvotes: 4