473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Using ParamArray()

ADezii
8,834 Expert 8TB
Generally, the number of Arguments in a Procedure Call must be the same as in the Procedure Specification. If a Procedure requires 3 Arguments in its Specification, when calling the Procedure you must pass exactly 3 Arguments which should be of the specific Data Type and in the correct order as defined by the Procedure itself. A simple case will illustrate this point:
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateInterest(curPrincipal As Currency, sngRate As Single, lngTermInMonths As Long) As Currency
  2.   fCalculateInterest = curPrincipal * sngRate * lngTermInMonths
  3. End Function
  4.  
To the fCalculateInterest() Function, you should pass a CURRENCY, SINGLE, and LONG value, and the Function will then return a CURRENCY Data Type.

A typical call to this Function may look something like:
Expand|Select|Wrap|Line Numbers
  1. Dim curInterest As Currency
  2. curInterest = fCalculateInterest(75000, .0675, 240)
  3.  
By using the ParamArray Keyword, you can specify that a Procedure will accept an arbitrary number of Arguments. Each Argument to a ParamArray Parameter can be of a different Data Type. The Parameter itself must be declared as an Array of Type Variant. When the call is made, each Argument supplied in the call becomes a corresponding element of the Variant Array. Again, a simple code segment will illustrate this point. Several calls will be made to the fAverageNumbers() Function, each with a varying number of Arguments, some even with invalid Arguments. The Function definition along with several calls and associated Outputs will be demonstrated:
Expand|Select|Wrap|Line Numbers
  1. Public Function fAverageNumbers(ParamArray varNumbers())
  2. Dim varX, varY, intNumsToAverage As Integer
  3.  
  4. intNumsToAverage = 0
  5.  
  6. For Each varX In varNumbers
  7.   If IsNumeric(varX) And Not IsNull(varNumbers) Then
  8.     varY = varY + varX
  9.     intNumsToAverage = intNumsToAverage + 1
  10.       fAverageNumbers = varY / intNumsToAverage
  11.   Else
  12.   End If
  13. Next
  14. End Function
  15.  
Sample Calls and Outputs:
Expand|Select|Wrap|Line Numbers
  1. Debug.Print fAverageNumbers(20, 80, 50)  'produces 50
  2. Debug.Print fAverageNumbers(2, 4, 8, 16, 32, 64, 128, 256, 512)  'produces 113.555555555556
  3. Debug.Print fAverageNumbers(98.765, 34.988, 1004.56)  'produces  379.437666666667 
  4. Debug.Print fAverageNumbers(Null, "Number", 500, 100)  'produces 300
  5. Debug.Print fAverageNumbers(16)  'produces 16
  6.  
NOTE: In my humble opinion, the ParamArray() Statement is an extremely valuable and versatile programming tool which can be used whenever the number of Arguments to be passed to a Procedure, as well as their Data Types are not known.
Nov 12 '07 #1
0 22970

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom | last post by:
Hi everyone In VB6,paramarray parameter is passed using byref,but in VB.NET,using byval.How can I get back the changed value of the paramarray parameter My work case in vb.net like this...
0
by: Cort | last post by:
I have a project that I am trying to upgrade that uses VB6 components and ASP. I have gotten to the point when I have been able to upgrade the project to VB.NET, compile and register it for use by...
2
by: Edlueze | last post by:
Greetings: I have two functions and I would like to pass the ParamArray gathered from one function to the other function. For the purposes of this post, let's say that they are calculating...
2
by: Bruno Miousse | last post by:
I am trying to invoke a COM component (written in VB6) which accepts parameter of ParamArray data type. What would be the proper syntax in C# for passing ParamArray in VB6? Thanks.
8
by: Tubs | last post by:
I know it might seem weird but i have a need to pass the contents of a paramarray from one sub to another that also takes a paramarray. Problem is i want to pass it exactly as it came in to the...
6
by: Peter | last post by:
Hi everyone In VB6,paramarray parameter is passed using byref,but in VB.NET,using byval.How can I get back the changed value of the paramarray parameter My work case in vb.net like this...
0
by: djpirra | last post by:
Hi, I managed to create a Shared Add-in in Vs.net 2003 and create all the menus, UDFS and all that.... The only 2 problems i am having now are: 1 - The formulas seems to work only at second...
3
mafaisal
by: mafaisal | last post by:
End of parameter list expected. Cannot define parameters after a paramarray parameter. Public Sub F_Bind(ByVal StrSql As String, ByVal ParamArray Ctrls() As Object,Optional ByVal Row as Integer...
14
mafaisal
by: mafaisal | last post by:
Hello Experts See My Code Private sub F_Clear(Paramarray Cntr()) dim i% For i=0 to ubound(cntr) cntr(i).text = "" next End sub
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.