//
you're reading...
.NET 2.0

Property w/ Events Snippets

Update! It turns out I didn’t compile the code produced by the snippet after I finished flushing it out so the event.GetInvocationList() line would error because it didn’t cast the returned array to the right type. I also forgot to handle the case where no events are registered to the property change notifications (ie event == null). I appear to be batting 0 for 3 here, I found yet another mistake and fixed it. Now the On* methods conform to the standard event handling procedure. I was leaving in the sender parameter when that shouldn’t be there. New version uploaded and the sample code has been updated as well.

Here is the snippet I mentioned in my last post.

The snippet format isn’t all the exciting, but I do like that we can tab through the “fields” to update each one. Copy and Paste programming at its finest! đŸ˜›

prop_event.zip

Installation is simple, extract the .snippet file to some place on your computer, then open the Code Snippet Manager in Visual Studio (under the Tools menu). Click Import at the bottom and select the prop_event.snippet file you extracted. Tell it what folders to place the snippet under and tell it OK.

In VC# Express intellisense will list multiple occurances of the same snippet so I would only select one folder as the destination.

Unfortunately they don’t give us the ability to remove snippets yet, so to remove them you’ll have to browse to the directory shown in the manager and manually delete the file. Changes are immediate if you aren’t in the manager.

In the extended entry you can see the result of this snippet

#region MyProperty Property

private type myVar;
public type MyProperty
{
    get
    {
        return myVar;
    }
    set
    {
        if( myVar != value )
        {
            CancelEventArgs cancel = new CancelEventArgs( false );
            OnMyPropertyChanging( cancel );

            if( cancel.Cancel )
                return;

            myVar = value;

            OnMyPropertyChanged( EventArgs.Empty );
        }
    }
}

public event CancelEventHandler MyPropertyChanging;
protected void OnMyPropertyChanging( CancelEventArgs e )
{
    if( MyPropertyChanging == null )
        return;

    CancelEventHandler[] handlers = (CancelEventHandler[]) MyPropertyChanging.GetInvocationList();

    foreach( CancelEventHandler eh in handlers )
    {
        eh( sender, e );

        if( e.Cancel )
            break;
    }
}

public event EventHandler MyPropertyChanged;
protected void OnMyPropertyChanged( EventArgs e )
{
    if( MyPropertyChanged == null )
        return;

    EventHandler[] handlers = (EventHandler[]) MyPropertyChanged.GetInvocationList();

    foreach( EventHandler eh in handlers )
    {
        eh( sender, e );
    }
}

#endregion // MyProperty Property
Advertisements

About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archive

%d bloggers like this: