Dave Sexton's Blog

on software development using C# and the Microsoft .NET Framework
Welcome to Dave Sexton's Blog Sign in | Join | Help

Generic Event Member Snippet

File Details
Downloads: 175 File Size: 2.3kB
Posted By: Dave Sexton Views: 571
Date Added: Mon, Mar 19 2007

My snippets are provided "as is", with no warranty of any kind, either express or implied.  Use at your own risk.

Creates the canonical event member declaration and corresponding method for invocation. EventHandler<TEventArgs> is used as the delegate and TEventArgs has been tokenized. The code produced by this snippet is thread-safe.

Shortcut: eventg

For more information on my C# Code Snippets see my blog entry: Custom C# Code Snippets.  All of my snippets may be downloaded at once here.

Usage

This snippet is intended to be used as expansion or surrounds with from within the body of a class definition.

Example Output

The following code illustrates this snippet's output using the default token values:

private readonly object TextChangedEventLock = new object();
private EventHandler<EventArgs> TextChangedEvent;

/// <summary>
/// Event raised after the <see cref="Text" />  property value has changed.
/// </summary>
public event EventHandler<EventArgs> TextChanged
{
  add
  {
    lock (TextChangedEventLock)
    {
      TextChangedEvent += value;
    }
  }
  remove
  {
    lock (TextChangedEventLock)
    {
      TextChangedEvent -= value;
    }
  }
}

/// <summary>
/// Raises the <see cref="TextChanged" /> event.
/// </summary>
/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
protected virtual void OnTextChanged(EventArgs e)
{
  EventHandler<EventArgs> handler = null;

  lock (TextChangedEventLock)
  {
    handler = TextChangedEvent;

    if (handler == null)
      return;
  }

  handler(this, e);
}

Filed under: , , ,
Comments
No comments exist for this file.

Add Comment

Name (required)
Web Site (optional)
Comment (required)
Add
Powered by Community Server (Personal Edition), by Telligent Systems