Passing arrays as parameters

72 Observe that even though the variable y is passed by value to the TestByVal method, one of its members nevertheless is updated. In this case, ByVal merely keeps the reference in y from being overwritten by another reference. attribute_list Specifies a list of custom attributes to apply to the parameter. Attributes are discussed later in this chapter. parameter_name Specifies the name of the parameter. As type_name Specifies the data type of the parameter. When the method is called, the type of the actual argument must be compatible with the type of the parameter. The As type_name element is optional if Option Strict is off; otherwise, it is required. If it is omitted, Object is assumed. constant_expression Specifies a constant expression that specifies what value the parameter should take if no actual argument is provided. This is permitted only on optional parameters.

2.14.6.2 Passing arrays as parameters

To declare a parameter as able to receive an array, include parentheses after the parameter name in the declaration. The caller leaves off the parentheses when naming the actual argument. For example: Public Shared Sub SomeMethodByVal x As String Dim str As String For Each str In x Console.WriteLinestr Next End Sub Public Shared Sub TestSomeMethod Dim a5 As String a0 = First a1 = Second a2 = Third a3 = Fourth a4 = Fifth SomeMethoda End Sub In the SomeMethod method, parameter x represents an array of String objects. In the TestSomeMethod method, a String array is allocated, its elements are assigned, and the array as a whole is passed to the SomeMethod method, which then prints the arrays contents. All array types are reference types. That means that when passing an array as a parameter, only a reference to the array is passed. Because the target method receives a reference to the array, the array elements can be changed by the method, even if the array reference was passed by value. If the array reference is passed by reference, the array reference itself can be changed by the method. For example, the method could allocate a new array and return it through the ByRef parameter, like this: Public Shared Sub DumpArrayByVal x As String 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