Tech Tock

Time is of the essence.

INotify Snippet Update

Here’s an update to my INotify snippet.  The only change is that I took out an unnecessary ref from the CheckPropertyChanged signature.  Now I’m hosting the code at google so its easier to keep updated.

protected bool CheckPropertyChanged<T>(string propertyName, ref T oldValue, (no ref here)  T newValue)
Advertisements

September 15, 2010 Posted by | Uncategorized | , , , , | Leave a comment

Visual Studio INotifyPropertyChanged Snippet

I just made my first Visual Studio snippet.  It’s really easy.

There’s a consensus developing that says dependency properties are usually too heavyweight and people are using INotifyPropertyChanged instead.  I miss the snippet that creates a dependency property, so I made a similar snippet for INotifyPropertyChanged.  You can download the code here.

To make a snippet, just go to Tools / Code Snippets Manager and find a snippet to start with.  I started with the “Define a Dependency Property” snippet since I was doing almost the same thing.  The Snippets Manager will tell you where that file is:

image

Go to the file system and copy the file to your My Code Snippets folder.  If you highlight the folder in the Code Snippet Manager, it will tell you where it is — you can copy the path from the location field.  The file is an xml file, but needs to be named “.snippet”.

Editing the file is very straightforward and the snippet will be available immediately – no restart needed.

One interesting thing about the code is that I’m using a simple method to do all the work:

I basically combined this CodeProject work with Jeff Yates object.Equals idea from here in order to handle both reference and value types in a single call:

Here’s how the snippet comes out by default (the myNames and type are the replaceable fields):

int _myField;

public int MyProperty
{
    get { return _myField; }
    set
    {
        CheckPropertyChanged("MyProperty",
            ref _myField, ref value);
    }
}

This is the method that does a check and notify in the base class:

protected bool CheckPropertyChanged<T>
      (string propertyName, ref T oldValue, ref T newValue)
      {
            if (!Equals(oldValue, newValue))
            {
                oldValue = newValue;
                RaisePropertyChanged(propertyName);
                return true;
            }
            return false;
        }

February 11, 2010 Posted by | Uncategorized | , , , , , | 3 Comments