This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

VS 2005+

Visual Studio Method Stub Generation

When writing code, it can be useful to refer to methods that do not yet exist, with the intention of adding these methods later. Using Visual Studio's Generate Method Stub command, these methods can be automatically generated.

Generate Method Stub Command

The Generate Method Stub command can be accessed in Visual Studio by simply right-clicking any method that is referenced in code but has not yet been declared. The command appears on a context-sensitive menu. This can be seen in the diagram below:

Visual Studio Generate Method Stub Menu Option

In the above diagram a call is made to a method named "CalculateVolume". This intention of the programmer is that this method will accept the width, height and depth of a cuboid as integer parameters and return the volume of the same cuboid as another integer. When the method name is right-clicked, Visual Studio is able to determine the parameter and return types from the variables used, so selecting the command adds the following method to the current class.

private int CalculateVolume(int width, int height, int depth)
    throw new Exception("The method or operation is not implemented.");

Creating a method in this way allows the program to continue compiling but without breaking the programmer's concentration by requiring the method is manually created. Note that the new method contains a single line of code that throws an exception indicating that the method requires implementation. This ensures that if the method is forgotten, the mistake is obvious.

The Generate Method Stub can also be used to generate methods that are in other classes and that are either static or instance methods. Visual Studio determines which option to use according to the prefix to the new method's name.

30 June 2008