Visual Studio Hack : Changing the default method stub

Programmers who have used Visual Studio to code in C# will know the feature of generating a method stub when the method is not defined but invoked. For those who do not know what I am talking about: When you are programming, you make a call MyClass.SayHi() and suppose MyClass class is defined but does not have a method called SayHi. Visual Studio (+ Intellisense) provides a very useful feature called Generating a method stub. You can right click on the SayHi method and say ‘Generate stub’. Visual Studio adds this method in MyClass.cs (or whichever cs file the class is defined in) and adds a code snippet something like this:

internal void SayHi()
{
throw new Exception(“”The method or operation is not implemented.””);
}

Let’s say you do not want to throw an exception, but just want to to print out a message saying that the method is not implemented. How do you do that? This post answers this question.

Open the file MethodStub.snippet which can be found under “<Visual Studio Install Directory>\VC#\Snippets\1033\Refactoring\”.

This is the culprit which is teaching your little code all bad things. Replace this line with:

$end$Console.WriteLine(“The method or operation is not implemented.”);

Save the file and you are done. In case, you want to revert back to the original snippet, make sure you take a backup of the file before saving.

So, next time you generate a method stub, you shouldn’t have a throw exception statement.

Be aware that though this code snippet will work well for void functions, there will be a compilation error when you generate a method stub for methods which return a value. You will have to manually plug in a return statement in the generated stub.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: