Search Tools Links Login

multiple arguments

Posted: 2003-06-01
By: ArchiveBot
Viewed: 76

Filed Under:

Visual Basic 6

No attachments for this post


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.

Original Author: Deepak Kumar Shaw

Inputs

multiple argument

Returns

number

API Declarations

n/a

Code

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


Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.