Tuesday, March 1, 2011

Is there a way to not create an explicit EventHandler delegate in Visual Studio?

Visual Studio likes to be helpful when typing:

Event +=

by generating code like:

Event += new EventHandler(EventClassName_Event);

void EventClassName_Event(object sender, EventArgs e)
{
    throw new System.NotImplementedException();
}

Ideally I would like to remove the explicit delegate and add an explicit private. Like so:

Event += EventClassName_Event;

private void EventClassName_Event(object sender, EventArgs e)
{
    throw new System.NotImplementedException();
}

I've looked to see if there is a snippet, but found nothing. Any suggestions? I do have ReSharper installed if there is a way that it can do this.

From stackoverflow
  • With Resharper if I type:

    myObject.SomeEvent +=
    

    then hit Ctrl-Shift-Space, I get the option to create a method or a delegate (or to use an existing method). I think this is what you want.

    Todd White : Hmm, ctrl-shift-space isn't doing anything. Maybe I need to change a setting?
    cbp : Hmm definitely works for me. It's called "Smart Completion" (http://www.jetbrains.com/resharper/features/coding_assistance.html)
    Todd White : When I go to tools -> options -> Environtment -> Keyboard it looks like ctrl+shift+space is assigned to Edit.ParameterInfo. Maybe it should be something different?
    Todd White : Looks like on my machine it was setup to be Ctrl+Alt+Space, not sure why that was chosen. The command that is run is "ReSharper.ReSharper_CompleteCodeSmart".

0 comments:

Post a Comment