Variable-length parameter lists Main method

73 Dim str As String For Each str In x Console.WriteLinestr Next End Sub Public Shared Sub CreateNewArrayByRef x As String Dim newval7 As String newval0 = 1st newval1 = 2nd newval2 = 3rd newval3 = 4th newval4 = 5th newval5 = 6th newval6 = 7th x = newval End Sub Public Shared Sub DoTest Set up a five-element string array and show its contents. Dim a5 As String a0 = First a1 = Second a2 = Third a3 = Fourth a4 = Fifth Console.WriteLinea before calling the CreateNewArray method: DumpArraya Now pass it to the CreateNewArray method and then show its new contents. CreateNewArraya Console.WriteLine Console.WriteLinea after calling the CreateNewArray method: DumpArraya End Sub In this code, the DoTest method creates a five-element string array and passes it to DumpArray to show the arrays contents. The DoTest method then calls CreateNewArray, which allocates a new string array—this time with seven elements. It would not be possible, however, to pass back an array with a different number of dimensions, because the parameter is explicitly declared as one- dimensional. Visual Basic .NET considers the dimensionality of an array to be part of its type, but the size of any particular dimension is not part of the arrays type.

2.14.6.3 Variable-length parameter lists

Some methods need to take a variable number of arguments. For example, a function to compute the average of the numbers passed to it should accommodate as few or as many numbers as needed. Visual Basic .NET provides this capability through parameter arrays . A parameter array is a parameter that to the method looks like an array but to the caller looks like a variable number of parameters. Here is the average-calculation method just mentioned: Public Shared Function AvgParamArray ByVal Numbers As Integer As Double Dim sum As Integer = 0 Dim count As Integer = 0 Dim n As Integer For Each n In Numbers 74 sum += n count += 1 Next Return sum count End Function This method declares only a single parameter—an array of Integers. However, it includes the ParamArray keyword in the declaration, which tells the compiler to allow calls such as this: Compute the average of some numbers. Dim d As Double = Avg31, 41, 59, 26, 53, 58 Its worth noting that an actual array can be passed through the ParamArray parameter—something that wasnt possible in Visual Basic 6. For example: Compute the average of some numbers. Dim args As Integer = {31, 41, 59, 26, 53, 58} Dim d As Double = Avgargs

2.14.6.4 Main method

When an executable application is compiled, some code must be identified as the startup routine. This portion is what is executed when the application is run. The Visual Basic .NET compiler looks for a method named Main to fulfill this need. In .NET, all code exists as methods within classes, even the Main method. To make it accessible without having to instantiate a class, the Main method must be declared as shared. For example: Public Class App Public Shared Sub Main ... End Sub End Class The name of the class is not important. At compile time, the Visual Basic .NET compiler looks for a public shared subroutine named Main somewhere in the code. If more than one class has such a method, the developer must specify which one to use by setting the startup object in the Project Properties dialog box. If youre using the command-line compiler, specify the desired startup object with the main:class switch. A programs Main method can also appear within a Visual Basic .NET module not to be confused with .NET modules, which are described in Chapt er 3 . Because Visual Basic .NET modules are classes wherein everything is shared, the Shared keyword is not used in such a declaration: Module App Public Sub Main ... End Sub End Module

2.14.6.5 Implementing interface methods