VS 2010: Generate From Usage
Posted
Thu, Apr 1 2010 10:59
by
Deborah Kurata
One of the very useful new features in Visual Studio 2010 is the ability to generate a function declaration when writing the code that calls that function. This allows you to define your functions as you need them, but define the implementation details of the function at a later time.
Say you are defining an event and want the event to call a function.
In C#:
You have not yet defined this function, so you get a squiggly line under the function. But you also get an option marker (blue underline). Hover over the marker and you get an option icon. Click on the icon and you get code generation options:
Click on the option and Visual Studio automatically generates the stub method for ValidateData.
private void ValidateData(bool p)
{
throw new NotImplementedException();
}
Instead of using the icon, you can right-click on the function name and select Generate | Method Stub to generate the method.
In VB:
You have not yet defined this function, so you get a squiggly line under the function. But you also get an Error Correction marker (red underline). Hover over the marker and you get an Error Correction icon.Click on the icon and you get error correction options:
Click on the option and Visual Studio automatically generates the stub method for ValidateData.
Private Sub ValidateData(ByVal p1 As Boolean)
Throw New NotImplementedException
End Sub
Using this technique, your code compiles without having to immediately define the function. The function throws an exception at runtime so you don't forget to add the implementation.
Use this technique whenever you want to write the structure of your code without being interrupted to fill out the implementation details.
This technique is especially useful if you like to follow a test-driven development (TDD) approach where you create the tests before the code.
Enjoy!