.Net lambda expression-- where did this parameter come from?
Posted
by
larryq
on Stack Overflow
See other posts from Stack Overflow
or by larryq
Published on 2011-11-30T17:42:51Z
Indexed on
2011/11/30
17:50 UTC
Read the original article
Hit count: 276
I'm a lambda newbie, so if I'm missing vital information in my description please tell me. I'll keep the example as simple as possible.
I'm going over someone else's code and they have one class inheriting from another. Here's the derived class first, along with the lambda expression I'm having trouble understanding:
class SampleViewModel : ViewModelBase
{
private ICustomerStorage storage = ModelFactory<ICustomerStorage>.Create();
public ICustomer CurrentCustomer
{
get { return (ICustomer)GetValue(CurrentCustomerProperty); }
set { SetValue(CurrentCustomerProperty, value); }
}
private int quantitySaved;
public int QuantitySaved
{
get { return quantitySaved; }
set
{
if (quantitySaved != value)
{
quantitySaved = value;
NotifyPropertyChanged(p => QuantitySaved); //where does 'p' come from?
}
}
}
public static readonly DependencyProperty CurrentCustomerProperty;
static SampleViewModel()
{
CurrentCustomerProperty = DependencyProperty.Register("CurrentCustomer", typeof(ICustomer),
typeof(SampleViewModel), new UIPropertyMetadata(ModelFactory<ICustomer>.Create()));
}
//more method definitions follow..
Note the call to NotifyPropertyChanged(p => QuantitySaved)
bit above. I don't understand where the "p" is coming from.
Here's the base class:
public abstract class ViewModelBase : DependencyObject, INotifyPropertyChanged, IXtremeMvvmViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged<T>(Expression<Func<ViewModelBase, T>> property)
{
MvvmHelper.NotifyPropertyChanged(property, PropertyChanged);
}
}
There's a lot in there that's not germane to the question I'm sure, but I wanted to err on the side of inclusiveness.
The problem is, I don't understand where the 'p' parameter is coming from, and how the compiler knows to (evidently?) fill in a type value of ViewModelBase from thin air?
For fun I changed the code from 'p' to 'this', since SampleViewModel inherits from ViewModelBase, but I was met with a series of compiler errors, the first one of which statedInvalid expression term '=>'
This confused me a bit since I thought that would work.
Can anyone explain what's happening here?
© Stack Overflow or respective owner