Lately I’ve been speaking at local user groups about many of the exciting new features in Visual Studio 2012. Since there is so much to know and learn about VS 2012, I decided to create a collection of blog posts with many of the tips. This collection of posts will include features that are new to VS 2012 as well as some existing features as well. To view all of the VS 2012 tips posted so far, click here.

I think (I hope) it is a well-known fact that Visual Studio will automatically complete your event handlers for you. The problem is, I’ve never liked the way it did this and I always went in and cleaned them up. Not any more!

What am I talking about? Here’s a sample. In this case, I have a class Foo that has an event SomethingHappened.

    public class Foo
    {
        public event EventHandler SomethingHappened;

        public void OnSomethingHappened(EventArgs e)
        {
            EventHandler handler = SomethingHappened;
            if (handler != null) handler(this, e);
        }
    }

In another class, I have an instance of Foo named foo. To wire up that event, all I need to do is type:

foo.SomethingHappened += [TAB][TAB]    (I hope you know that I mean hit the “tab” key twice Smile)

When you do that in Visual Studio 2010 (and prior), you will get the following results:

public class SomeService
{
    private Foo foo = new Foo();
    public SomeService()
    {
        foo.SomethingHappened +=new EventHandler(foo_SomethingHappened);
    }

    void foo_SomethingHappened(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

What happened here? Visual studio added a method (foo_SomethingHappened) and attached it to my handler for me. Thanks!

But this gets even better when you use Visual Studio 2012! Doing the same thing will get you this:

    public class SomeService
    {
        private Foo foo = new Foo();
        public SomeService()
        {
            foo.SomethingHappened += foo_SomethingHappened;
        }

        void foo_SomethingHappened(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }

 

OK, it isn’t thatdifferent. But the “new EventHandler” text is not there. It isn’t needed, of course so it rightfully isn’t there. I try to remove as much extra text from my code to make it more readable and I’m glad the Visual Studio team is thinking along the same lines. Actually, a lot of the time I clean this up even more by removing the object name from the event handler, in this case “foo_”. It might be needed sometimes, for clarity or, of course, if you need to specify which object the event is for. But in this case, I think it is just overly verbose. I prefer this:

    public class SomeService
    {
        private Foo foo = new Foo();
        public SomeService()
        {
            foo.SomethingHappened += SomethingHappened;
        }

        void SomethingHappened(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }

I hope you find this tip helpful. To see more VS 2012 Tips, just click here.

One thought on “Visual Studio 2012 Tip: Better Event Handler Code Completion

  1. Pingback: Dew Drop – February 18, 2013 (#1,500) | Alvin Ashcraft's Morning Dew

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>