Notify Property Changed
This is my favourite because I use it often and it does a lot of work for me.
Shortcut: npc
Available in: C# 2.0+ where expression is allowed.
if (value != _$LOWEREDMEMBER$)
{
_$LOWEREDMEMBER$ = value;
NotifyPropertyChanged("$MEMBER$");
}
Macros:
- MEMBER - Containing member type name. Not editable. Note: make sure this one is first in the list.
- LOWEREDMEMBER - Value of MEMBER with the first character in lower case. Not editable.
Usage:
Inside a property setter like this:
private string _dateOfBirth;
public string DateOfBirth
{
get { return _dateOfBirth; }
set
{
npc<--tab from here
}
}
It assumes that your backing variable starts with an "_". Replace this with whatever you use. It also assumes that you have a property change method something like this:
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
In reality, the version of this I use is lambda based ('cos I loves my lambdas!) and produces the below. The principles are the same as the above.
public decimal CircuitConductorLive
{
get { return _circuitConductorLive; }
set { Set(x => x.CircuitConductorLive, ref _circuitConductorLive, value); }
}
That's when I'm not using the extremely elegant and useful PostSharp to do the whole INotifyPropertyChanged thing for no effort, that is.