Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set a breakpoint when variable is getting a specific value in .NET?

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?

like image 997
Delashmate Avatar asked Sep 20 '11 16:09

Delashmate


People also ask

How do you set a conditional breakpoint?

To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.

How do I add a conditional breakpoint in Visual Studio?

Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

How do you set a breakpoint which only occurs when a set of conditions is true?

Use conditional breakpoints to conditionally stop program execution. Breakpoints normally stop the execution every time a certain line or function is reached. However, using the condition keyword, a breakpoint will only be activated if a certain condition is true.

Is it possible to change the value of a variable while debugging in C#?

'Yes' it possible to change the "value of a variable" while debugging a "c# application".


1 Answers

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.

  • Set a break point at the point the variable changes
  • Right click on the break point and select "Condition"
  • Type in the conditional like "theNewValue == 42"

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.

like image 128
JaredPar Avatar answered Sep 19 '22 06:09

JaredPar