views:

66

answers:

3

When writting codes, if we typed a undefined references, pressing Ctrl+. will bring up the generating-code list box which says generate field/property/method. I'm looking for a way to generate a local variable but I don't know how to.

To make it more clearer, I'll give an example. Say we have a method

void MyMethod(MyType param)
{
    //do something
}

Then in another method of the same class I write:

MyMethod(undefinedObject);

I wish to be able to generate the below codes automatically (of course through Ctrl+. when placing the cursor under 'undefinedObject'):

var undefinedObject = new MyType();
MyMethod(undefinedObject);

Hope you have some work-around for me. Thanks!

[Edit] At the moment, I use the generated field. Then cut the line declaring the field and paste it on the line where it should be in my code. This routine is time-consuming and quite boring. So I ask here to look for a better way/addon to do that. Prefer a free tool, not Resharper I hope.

+1  A: 

Resharper will offer this via alt+enter, enter

Marc Gravell
I think he is looking for how to create an MEF component that can be installed and such code can be extended.
Akash Kava
@Akash: Where do you see the op mentioning about *writing a component* or of *extensibility*?
KMan
A: 

Visual Studio 2010 offers MEF (Managed Extendibility Framework) in which you can create your own context menu extension after installing Visual Studio 2010 SDK, and you can write your code, it will not be very easy and straight forward, but you can look into SDK documentation and samples offered by SDK, you will get some idea of how to customize and create custom behaviour.

Akash Kava
A: 

One way is to create your own or edit an existing Method Stub - Body snippet that comes with Visual Studio. You can access it via Tools-->Refactoring menu option.

You may require:

  1. A Snippet Editor
  2. A manual step by step Howto

Also, this article may help.

KMan