80Microsoft Visual Studio 2010: A Beginner’s Guide

Figure 3-2 The VB sub snippet template

VB (Calc.vb):

Public Class Calc

Public Function SquareInt(ByVal number As Integer) As Integer

Return number * number

End Function

End Class

For the C# example, notice how the return type of the SquareInt method is type int, rather than the keyword void that was used in our methods before. Whenever you specify a return type, the method must return something whose type is the same as the return type declared. In the preceding example, the return type is declared as int; therefore, the method guarantees that the result of the calculation is type int. The Main method has

a couple of statements that invoke this method and display the results to the console. In the VB example, the method is now a Function. Sub methods don’t return values.

Notice how the function signature appends As Integer after the parameter list, which indicates that the return type of the function is Integer.

Method Snippets

C# doesn’t have snippets for writing methods (although you could create your own snippets), but VB does. In VB, type Sub, TAB, TAB; producing the template shown in Figure 3-2; or Fun, TAB, TAB; producing the template shown in Figure 3-3.

Figure 3-3 The VB function snippet template

Page 103
Image 103
Microsoft 9GD00001 manual Method Snippets, VB sub snippet template