Generally, all the arguments that a procedure expects are listed in the procedure’s definition, and the program that calls the procedure must supply values for all arguments. On occasion, however, you might not know how many arguments will be passed to the procedure. Procedures that calculate averages or, in general, process multiple values can accept from a few to several arguments whose count is not known at design time. VB 2008 supports the ParamArray keyword, which allows you to pass a variable number of arguments to a procedure.
Let’s look at an example. Suppose that you want to populate a ListBox control with elements. To add an item to the ListBox control, you call the Add method of its Items collection as follows:
ListBox1.Items.Add("new item")Code language: VB.NET (vbnet)
This statement adds the string new item to the ListBox1 control. If you frequently add multiple items to a ListBox control from within your code, you can write a subroutine that performs this task. The following subroutine adds a variable number of arguments to the ListBox1 control:
Sub AddNamesToList(ByVal ParamArray NamesArray() As Object)
Dim x As Object
For Each x In NamesArray
End SubCode language: VB.NET (vbnet)
This subroutine’s argument is an array prefixed with the keyword ParamArray, which holds all the parameters passed to the subroutine. If the parameter array holds items of the same type, you can declare the array to be of the specific type (string, integer, and so on). To add items to the list, call the AddNamesToList() subroutine as follows:
AddNamesToList("Robert", "Manny", "Renee", "Charles", "Madonna")Code language: VB.NET (vbnet)
If you want to know the number of arguments actually passed to the procedure, use the Length property of the parameter array. The number of arguments passed to the AddNamesToList() subroutine is given by the following expression:
NamesArray.LengthCode language: VB.NET (vbnet)
The following loop goes through all the elements of the NamesArray and adds them to the list:
Dim i As Integer
For i = 0 to NamesArray.GetUpperBound(0)
Next iCode language: VB.NET (vbnet)
VB arrays are zero-based (the index of the first item is 0), and the GetUpperBound method returns the index of the last item in the array.
A procedure that accepts multiple arguments relies on the order of the arguments. To omit some of the arguments, you must use the corresponding comma. Let’s say you want to call such a procedure and specify the first, third, and fourth arguments. The procedure must be called as follows:
ProcName(arg1, , arg3, arg4)Code language: VB.NET (vbnet)
The arguments to similar procedures are usually of equal stature, and their order doesn’t make any difference. A function that calculates the mean or other basic statistics of a set of numbers, or a subroutine that populates a ListBox or ComboBox control, are prime candidates for implementing this technique. If the procedure accepts a variable number of arguments that aren’t equal in stature, you should consider the technique described in the following section. If the function accepts a parameter array, this must the last argument in the list, and none of the other parameters can be optional.