3

Say I have this auto-implemented property in class ClassName: public int Counter{ get; set; }

I have not successfully been able to have a conditional breakpoint on a C# auto-implemented property setter in Visual Studio 2013. Specifically, on the new value being set. (I would like to breakpoint it when it is set to a negative number, for example.)

I know there are other solutions, like breaking out the property so that it isn't an auto-implemented property, or breakpointing all places that set that property. But I would love to just be able to do it without tedious workarounds.

I have successfully breakpointed on an auto-implemented property setter using the following tip from https://stackoverflow.com/a/6713867/119418

Using Visual Studio 2008, 2010, 2012, 2013:

  1. Go to the Breakpoint window
  2. New->Break at Function…
  3. For the get, type: ClassName.get_Counter()

    For the set, type: ClassName.set_Counter(int)

You'll get a "No Source Available" when the breakpoint is hit, but you'll get the calling location in the call stack.

Community
  • 1
  • 1
Mafu Josh
  • 2,523
  • 1
  • 23
  • 25
  • Is your property type `int`? – Hamlet Hakobyan Dec 31 '14 at 19:48
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 31 '14 at 19:49
  • If you're trying to break on a property setter, you're probably doing something wrong. Which doesn't make much sense, as you're not doing anything, specifically (auto properties, btw). My mind is confused. –  Dec 31 '14 at 19:56
  • 1
    I want to break when a property value changes to a negative number, for example. And I don't know what line of code is setting it to this negative number. There are a lot of places it could be happening. I know I can conditionally breakpoint all those, and will if I have to. This is a pattern I have to do a lot in this software (that isn't mine). – Mafu Josh Dec 31 '14 at 20:11
  • I added the example of the auto property (of type int) – Mafu Josh Dec 31 '14 at 20:15

2 Answers2

0

im not 100% sure what your trying to do if i got this correct you just want to set a breakpoint to a property if someone sets it to a negative value

i could be be heading in the wrong direction here but are you looking for this kind of thing

private int _age;
public int Age
{
      get{ return _age;  }
      set{ 
        if(value < 0) 
        { throw somthing;} //add a breakpoint here 
        else{ _age = value;} 
         }
}// im writing this directly in the browser so forgive space indentation etc 

this may not be exactly what you want but i do see it as the easiest way to achieve what your asking

  • 2
    Converting from an auto property to a normal property is a workaround I might do, but requires (probably) stopping the current debug session and rebuilding, and then reverting when I'm done. If I could simply add a conditional breakpoint (like I can to non-auto properties), that would be great. – Mafu Josh Dec 31 '14 at 20:18
  • oo can you not just set a condition on the break point with a expression i<0 like shown in this pic https://mscblogs.blob.core.windows.net/media/scottgu/Media/image_1F841640.png and then after you hit that should get this box ive never tried but seems very logical to assume it can handle a expression as simple as <0 http://blog.dreamlabsolutions.com/image.axd?picture=WindowsLiveWriter/DebuggingtipsandtricksforVisualStudio_DD7D/image_18.png – michael.bourke Dec 31 '14 at 20:35
  • kind of feel silly like im preaching to the choir in sense that im sure some one would have suggested that if thats what your looking for ooo i see your problem after a google search it appears with auto best i got was "When you use automatic properties and you set the same breakpoint on accessing the property, you notice that nothing actually happens, which makes sense because there is only direct IL code, generated by the compiler behind the setter implementation, and no C# code, so the compiler has nowhere to go! Using watches might help you – michael.bourke Dec 31 '14 at 20:45
0

Not exactly the answer to the question (for Visual Studio 2013), but breakpointing on getters and setters is expected to work in Visual Studio 2015.

https://devblogs.microsoft.com/devops/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015/

ankostis
  • 8,579
  • 3
  • 47
  • 61
Mafu Josh
  • 2,523
  • 1
  • 23
  • 25