Thursday 2 December 2021

June-2019(8) Explain with example how you pass an array to procedure.

Parameter array in

A procedure can never be called with more arguments than that specified in the procedure declaration. However, using a parameter array, we can pass an array of values for an argument of a procedure. In addition, we need not know the number of elements in the parameter array when we define the procedure. The array size can be determined individually by each call to the procedure. The 'ParamArray' keyword is used to denote a parameter array. Rules to be followed while using a Parameter Array are:

We cannot use more than one parameter array in a procedure, and it must be the last argument in the procedure definition.
The parameter array must be passed by value and we should specify the ByVal keyword.

The code within the procedure must use the parameter array as a one-dimensional array. In addition, each element of the array must be of the same data type as the data type of ParamArray.
The parameter array is optional. The default value of a parameter array is an empty one-dimensional array. The parameter array must be the lone optional argument in the list of arguments for a procedure. All other arguments preceding the parameter array must be as signed value.

Passing arguments of a Parameter Array

While calling a procedure, we can pass the following arguments to a parameter array: An array that has the same element type as the parameter array. The arguments are separated by commas. We can also omit the ParamArray argument. If we omit the ParamArray argument, an empty array for the parameter array will be passed to the procedure. The following example illustrates the definition of a procedure with a parameter array.

Private Sub AccOrders(ByVal AccName As String, ByVal paramArray orders() As String)
Dim i as integer
Msgbox(AccName & "has orders:")
For i = 0 to UBound(orders)
MsgBox("Orders" & i & orders(i))
Next i
End Sub

We can make calls to the procedure AccOrders as the following.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


End Sub

No comments:

Post a Comment