Click here to Skip to main content
Licence CPOL
First Posted 7 Feb 2010
Views 5,836
Bookmarked 6 times

C# Property Class - Part 2

By Don Kackman | 7 Feb 2010 | Technical Blog
Commenter tonyt (from CodeProject) rightly points out that there are drawbacks to this approach: A C# Property Class.

1

2
1 vote, 50.0%
3

4
1 vote, 50.0%
5
4.00/5 - 2 votes
μ 4.00, σa 2.47 [?]
A Technical Blog article. View original blog here.[^]

Commenter tonyt (from CodeProject) rightly points out that there are drawbacks to this approach: A C# Property Class.

Why:

Because there is a mountain of core functionality in .NET that relies heavily on things like property access via reflection (like data binding) and via System.ComponentModel (e.g., TypeDescriptor), none of which support your take on implementing properties.

You can implement INotifyPropertyChanged on any class that offers a more efficient way to get notified about property changes, as it requires only one delegate for each listener, regardless of how many supported properties you have.

He's right. But I still want to explore this approach. What if we take the Property class and have it implement INotifyPropertyChanged?

public class Property<T> : INotifyPropertyChanged
{
    protected T _value = default(T);

    public Property()
    {
    }

    public Property(T value)
    {
        _value = value;
    }

    public event EventHandler Changed;

    public event PropertyChangedEventHandler PropertyChanged;

    public virtual T Value
    {
        get { return _value; }
        set
        {
            if ((value != null && !value.Equals(_value)) ||
                (_value != null && !_value.Equals(value)))
            {
                _value = value;
                OnChanged();                    
            }
        }
    }

    public override string ToString()
    {
        return object.ReferenceEquals(_value, null) ? string.Empty : _value.ToString();
    }

    protected virtual void OnChanged()
    {
        if (Changed != null)
            Changed(this, EventArgs.Empty);

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Value"));
    }

    public static implicit operator T(Property<t> property)
    {
        if (property == null)
            return default(T);

        return property.Value;
    }
}

Now we can create a class as follows:

class ViewModel
{
    public ViewModel()
    {
        Status = new Property<string>("");
    }

    public Property<string> Status { get; private set; }
}

Note the switch from a readonly field to a read-only property. WPF binding requires properties and does not work with fields.

And then we can bind to that in XAML as follows:

<TextBox Text="{Binding Path=Status.Value}"/>

(assuming that the DataContext of the TextBox is a ViewModel like the one above.)

That should go a little way to addressing tonyt's point (mostly because WPF data binding is so robust).

License

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

About the Author

Don Kackman

Team Leader
Starkey Laboratories
United States United States

Member
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10
It only went downhill from there.
 
Hey look, I've got a blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThe PropertyChanged event call does not receive the correct property name PinmemberJim McCurdy14:09 9 Feb '10  
GeneralRe: The PropertyChanged event call does not receive the correct property name PinmemberDon Kackman14:27 9 Feb '10  
QuestionIsnt this class available in .Net 4.0? Pinmembertheperm4:44 9 Feb '10  
AnswerRe: Isnt this class available in .Net 4.0? PinmemberDon Kackman9:59 9 Feb '10  
AnswerRe: Isnt this class available in .Net 4.0? [modified] PinmemberDon Kackman13:24 9 Feb '10  
AnswerRe: Isnt this class available in .Net 4.0? PinmemberDon Kackman13:31 9 Feb '10  
GeneralYes you can. Pinmembertonyt2:47 8 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 7 Feb 2010
Article Copyright 2010 by Don Kackman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid