6

Let's say I have this property

public ISetting Setting { get; set; }

How can I get breakpoint at the set? So that the program will pause when something is setting a value.

I try to make it this way

public IDatabaseConnectionSetting ConnectionSetting { 
    get; 
    set;
}

And put the breakpoint on the set; line, but still it doesn't work. The red breakpoint highlighter highlights the whole property declaration

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

3 Answers3

7

There's a better solution here: Can't set breakpoints on an auto-property setter ? Why?

Using Visual Studio 2008, 2010, 2012:

  1. Go to the Breakpoint window

  2. New->Break at Function…

  3. For the get, type: ClassName.get_CurrentFramesize()

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

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

I found this solution here: http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/b1dd0dc3-e9c1-402a-9c79-a5abf7f7286a

See also: Debugging automatic properties

Community
  • 1
  • 1
Bochu
  • 377
  • 3
  • 7
5

Use a full property rather than autoproperty.

The shortcut is propfull

private ISetting setting;

public ISetting Setting 
{ 
    get 
    { 
        return setting; 
    }
    set 
    { 
        setting = value; 
    }
} 

To use the code-snippet shortcut, type propfull and then press TAB twice.

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
  • I find it weird if I have to change my code to full property just to debug it – Louis Rhys Aug 25 '11 at 02:48
  • 1
    @Louis yes, I agree and hopefully someone will answer with a new technique that I didn't know existed. But I know a lot of devs who code like `var result = blah.Evaluate; return result;` specifically so they can put a breakpoint on the return line & inspect what's being returned. That's the same sort of thing. – Kirk Broadhurst Aug 25 '11 at 02:50
  • Ok. Just curious, what do you mean by press tab twice? usually I type it, tab, type, tab, type. – Louis Rhys Aug 25 '11 at 03:01
  • When I type it and press `TAB` (just once) nothing happens and I'll just typing (e.g. I'll end up with `propfullint`) - I have to press `TAB` twice, then I can enter the type, then `TAB`, then the field name etc. Don't know why, that's how I thought it was. – Kirk Broadhurst Aug 25 '11 at 03:12
2

No, you can't. Automatic properties are compiled like that one with backing store. I think there is no reason to allow breakpoints on them, because somewhere you must assign them, check your property there.

private bool TestProperty { get; set; }

is compiled like

[CompilerGenerated]
private bool <TestProperty>k__BackingField;
[CompilerGenerated]
private void set_TestProperty(bool value)
{
    this.<TestProperty>k__BackingField = value;
}
[CompilerGenerated]
private bool get_TestProperty()
{
    return this.<TestProperty>k__BackingField;
}
Petr Klíma
  • 146
  • 3
  • 4
    it could be set in any number of places - much more useful to break inside the setter than every place that calls the setter. – Kirk Broadhurst Aug 25 '11 at 03:52
  • I agree.That should be feature of VS. May i ask what is your use case when you need call setters from many places? Just curious, thanks. – Petr Klíma Aug 25 '11 at 07:11