by Deepak Kumar Shaw (9 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
Do you want to pass multiple arguments to your function/method?
There are two ways to do this using unbound array or you can use 'ParamArray'. ParamArray is really a professional way to proceed.
Here is the sample code to make sum of numbers to make you clear how to pass multiple argument to a function.
Inputs
multiple argument
Code Returns
number
API Declarationsn/a
Private Sub Command1_Click()
'*** 1ts Method ***
Dim i(5) As Integer
i(0) = 1: i(1) = 2: i(2) = 3: i(3) = 4: i(4) = 5: i(5) = 5:
MsgBox Add1(i)
'*** 2nd method ***
MsgBox Add(1, 2, 3, 4, 5, 5)
End Sub
'*** Here we are using an unbound array to pass int type of array like C/C++ ***
Private Function Add1(i() As Integer) As Integer
Dim tt: tt = Now()
Dim j As Long, sum As Long
For j = 0 To UBound(i)
sum = sum + i(j)
Next j
Debug.Print Now() - tt
Add1 = sum
End Function
'*** This Method use ParamArray for the multiple arguments ***
Private Function Add(ParamArray i()) As Long
Dim tt: tt = Now()
Dim sum As Long: sum = 0
For j = 0 To UBound(i)
sum = sum + i(j)
Next j
Debug.Print Now() - tt
Add = sum
End Function