473,395 Members | 2,079 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,395 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 22973

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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.