Wednesday, December 16, 2009

Code Snippet - Event Declaration

So I found myself needing to write a bunch of events and their calling method OnEvent. And I thought, well this is a bit ridiculous, surely there is a handy code snippet I could write that would automate the entire process.

And so...

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Public Event Declaration</Title>
      <Description>Snippet for automatic public Event and private OnEvent declaration.</Description>
      <Shortcut>eventpri</Shortcut>
      <Author>Alastair Pitts</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>EventArg</ID>
          <ToolTip>Replace with the event args.</ToolTip>
          <Default>EventArgs</Default>
        </Literal>
        <Literal>
          <ID>EventName</ID>
          <ToolTip>Replace with name of event.</ToolTip>
          <Default>MyEvent</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
        public event EventHandler<$EventArg$> $EventName$;
        
        private void On$EventName$(object sender, $EventArg$ e)
        {
            var temp = $EventName$;
            if (temp != null)
                temp(sender, e);
        }
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Protected Event Declaration</Title>
      <Description>Snippet for automatic public Event and protected OnEvent declaration.</Description>
      <Shortcut>eventpro</Shortcut>
      <Author>Alastair Pitts</Author>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>EventArg</ID>
          <ToolTip>Replace with the event args.</ToolTip>
          <Default>EventArgs</Default>
        </Literal>
        <Literal>
          <ID>EventName</ID>
          <ToolTip>Replace with name of event.</ToolTip>
          <Default>MyEvent</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
        public event EventHandler<$EventArg$> $EventName$;
        
        protected virtual void On$EventName$(object sender, $EventArg$ e)
        {
            var temp = $EventName$;
            if (temp != null)
                temp(sender, e);
        }
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

By typing the intellisense shortcut eventpri, you get the event with a private OnEvent method. This is suitable for sealed classes. The generated signature will look like this:
public event EventHandler<MyEventArgs> MyEvent;

private void OnMyEvent(object sender, MyEventArgse)
{
    var temp = MyEvent;
    if (temp != null)
        temp(sender, e);
}

If your creating a class that maybe inherited from, then the eventpro will be suitable as the OnEvent method can be overridden and acted upon. The generated signature will look like this:
public event EventHandler<MyEventArgs> MyEvent;

protected virtual void OnMyEvent(object sender, MyEventArgse)
{
    var temp = MyEvent;
    if (temp != null)
        temp(sender, e);
}

I hope this snippet is useful and allows developers to shave the required seconds off development time :)


The snippet file can be downloaded from the following link: Event.snippet
To install, copy to "C:\Users\[Username]\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets"

No comments:

Post a Comment