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

53,798

Solution 1

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.

Solution 2

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.

Has Changed option for breakpoint conditions

Solution 3

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.

Solution 4

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:

Enter image description here

Pick Condition...

Solution 5

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

Share:
53,798
Delashmate
Author by

Delashmate

I am studying computer seicnce in Tel Aviv college, this is my forth and last year, I also work a software engineer for just more than one year, I am starting to love programing :) I know very good: c, c++ and c# I also know: Networking: wcf, ice, tcp and udp using c, omnet++ Web: asp.net, html, javascript Gadjets: facebook, open-graph, ffmpeg, lego mindstrom Data format: xml, jason, ini, dicom, pgm OS : windows, Linux (programing to kernel) Logic Programing: Alloy,Z Hoping this year to learn deeply java, facebook, asp.net, and xna Delashmate

Updated on July 09, 2022

Comments

  • Delashmate
    Delashmate almost 2 years

    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?

  • Delashmate
    Delashmate almost 13 years
    do you know why data break point not available in managed languages?
  • JaredPar
    JaredPar almost 13 years
    @Delashmate it's a limitation in the CLR's debugging infrastructure. Visual Studio's hands are essentially tied until the CLR provides the capability
  • Delashmate
    Delashmate almost 13 years
    Ok, I hope it isn't so complicated question, why the CLR don't support this option?
  • JaredPar
    JaredPar almost 13 years
    @Delashmate it's just a time / value trade off. There isn't any functional reason they can't support it. Thus far the time it would take to implement it thus far has been to big compared to the value they believe it would provide. FWIW: I wish they would support it. But I would also prefer they provide ENC 64 bit over this.
  • Delashmate
    Delashmate almost 13 years
    Ok, now I understand, what I was looked for is to get notification when variable got changed in general, means to create data break point.. that way it's more general..
  • beppe9000
    beppe9000 over 9 years
    you could have a timer that checks for this
  • Lukas
    Lukas over 2 years
    Data breakpoints are availble since Visual Studio 2019 Preview 2. See my answer for the link to the devblog.