Click here to Skip to main content
13,736,952 members
Rate this:
 
Please Sign up or sign in to vote.
Hi. My question is, is there a difference between this:

private ScreenMenuCategory _selectedCategory;
public ScreenMenuCategory SelectedCategory
{
     get { return _selectedCategory; }
     set { _selectedCategory = value; RaisePropertyChanged(() => SelectedCategory); }
}


and this?

private ScreenMenuCategory _selectedCategory;
public ScreenMenuCategory SelectedCategory
{
    get { return _selectedCategory; }
    set { _selectedCategory = value; RaisePropertyChanged(SelectedCategory); }
}


Thanks in advance.
Posted 25-Sep-13 4:22am
Comments
Richard C Bishop 25-Sep-13 10:33am
   
Well, obviously there is a syntax difference. Do they both compile? Do they both do the same thing?
YourAverageCoder 25-Sep-13 10:39am
   
It's just a random code i made up. Obviously there are differences but i don't get them.
Richard C Bishop 25-Sep-13 10:44am
   
Very well, does the top example even compile?
Richard C Bishop 25-Sep-13 10:57am
   
I didn't think so. If RaisePropertyChanged() is a method, then the argument you are passing it is not valid code. What datatype does that method expect?
YourAverageCoder 25-Sep-13 11:01am
   
ValueClass class that has lots of string variables and properties.
YourAverageCoder 25-Sep-13 10:51am
   
No, it says it can't convert from some object to string[].
BillWoodruff 25-Sep-13 13:15pm
   
"It's just a random code i made up. Obviously there are differences but i don't get them."

Please do not post code you "made up" that you know does not compile !

However, if you have a piece of code you know works under some circumstances, for someone, and you are having problems getting it to work for you, then posting that code is fine.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

An Expression<tdelegate> represents the abstract syntax tree of the lambda expression. So you just have to analyze this syntax tree to find out the property name:

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
    var memberExpr = propertyExpression.Body as MemberExpression;
    if (memberExpr == null)
        throw new ArgumentException("propertyExpression should represent access to a member");
    string memberName = memberExpr.Member.Name;
    RaisePropertyChanged(memberName);
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.180920.1 | Last Updated 25 Sep 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100