473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing a ParamArray from one Function to another Function

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 averages (they're actually
processing a sequence of pairs of variants and the sequence is of
unknown length).

I want something like these two functions:

#1 Function AverageSequenceOfNumbers(ParamArray SequenceOfNumbers As
Variant) as Double
....
End Function

#2 Function AverageSequenceOfNumbersPlusOne(ParamArray
SequenceOfNumbers as Variant) as Double

AverageSequenceOfNumbersPlusOne = AverageSequenceOfNumbers(99,
SequenceOfNumbers)

End Function

The second function is brand new, but the first function is
tried-and-true code so I don't want to go back and strip out the
ParamArray in the first function and replace it with a regular Array.
In fact, I don't want to touch the first function at all (unless it is
really minor change). If there is not something obvious then is there a
work-around I can implement in the new second function?
Thanks,

Edlueze.

Nov 13 '05 #1
2 9564
Alas, this is no easy task - in fact, it's pretty much intractible.

My response to this issue has been to stop using Paramarray arguments, and
instead, take a variant array. The Array function, then makes it easy to pass
a custom-built array of values in-line from calling code. That this seems to
have been the logic of the ADO API as well, since many ADO methods accept
Array values passed to variant arguments.

Example based on your example (untested air code):

Function AverageSequenceOfNumbers( _
SequenceOfNumbers As Variant _
) as Double
Dim varNumItem As Variant
Dim varSumOfNums As Variant
Dim varItemCount As Long

varSumOfNums = 0&

For Each varNumItem in SequenceOfNumbers
varSumOfNums = varSumOfNums + varNumItem
Next

varItemCount = UBound(SequenceOfNumbers) - _
LBound(SequenceOfNumbers) + _
1

AverageSequenceOfNumbers = _
varSumOfNums / varItemCount

End Function

Function AverageSequenceOfNumbersPlusOne( _
SequenceOfNumbers as Variant _
) as Double
Dim varSequencePlusOne As Variant
Dim lngLBound As Long
Dim lngUBound As Long

varSequencePlusOne = SequenceOfNumbers
lngLBound = LBound(varSequencePlusOne)
lngUBound = UBound(varSequencePlusOne)

Redim Preserve varSequencePlusOne(lngLBound To lngUBound+1)
varSequencePlusOne(lngUBound) = 99

AverageSequenceOfNumbersPlusOne = _
AverageSequenceOfNumbers(varSequencePlusOne)

End Function

On 20 Jan 2005 22:12:53 -0800, "Edlueze" <ed*****@onegen.com> wrote:
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 averages (they're actually
processing a sequence of pairs of variants and the sequence is of
unknown length).

I want something like these two functions:

#1 Function AverageSequenceOfNumbers(ParamArray SequenceOfNumbers As
Variant) as Double
...
End Function

#2 Function AverageSequenceOfNumbersPlusOne(ParamArray
SequenceOfNumbers as Variant) as Double

AverageSequenceOfNumbersPlusOne = AverageSequenceOfNumbers(99,
SequenceOfNumbers)

End Function

The second function is brand new, but the first function is
tried-and-true code so I don't want to go back and strip out the
ParamArray in the first function and replace it with a regular Array.
In fact, I don't want to touch the first function at all (unless it is
really minor change). If there is not something obvious then is there a
work-around I can implement in the new second function?
Thanks,

Edlueze.


Nov 13 '05 #2
Thanks a lot Steve! You've taught me a lot about an area I knew little
about. I took your code for a test run and it worked perfectly first
time.

Based upon your example, I think I've come up with a way to preserve
backwards compatibility while adding the new functionality I need. It
essentially involves expanding the capability of the existing first
function to accept either a ParamArray or a regular Array, and then
have the new second function call the first with a regular Array - that
way I don't have to modify all of the other places the first function
is called from my application. This is done by building a consolidated
Array right at the top of the first function and then leaving the rest
of the code (hopefully) untouched.

I've tried to illustrate this below by modifying the original code you
provided me (changing the function names for the purpose of this post):

Function AverageSequenceOfNumbersFromParamArray( _
ParamArray SequenceOfNumbers() As Variant) As Double
Dim varNumItem As Variant
Dim varNumSubItem As Variant
Dim varSumOfNums As Variant
Dim varItemCount As Long
Dim SequenceOfNumbersArray() As Variant 'Builds an Array from the
parameters passed to the function

varItemCount = 0

For Each varNumItem In SequenceOfNumbers
If IsArray(varNumItem) Then
For Each varNumSubItem In varNumItem
ReDim Preserve SequenceOfNumbersArray(varItemCount)
SequenceOfNumbersArray(varItemCount) = varNumSubItem
varItemCount = varItemCount + 1
Next
Else
ReDim Preserve SequenceOfNumbersArray(varItemCount)
SequenceOfNumbersArray(varItemCount) = varNumItem
varItemCount = varItemCount + 1
End If
Next

varSumOfNums = 0&

For Each varNumItem In SequenceOfNumbersArray
varSumOfNums = varSumOfNums + varNumItem
Next

varItemCount = UBound(SequenceOfNumbersArray) - _
LBound(SequenceOfNumbersArray) + _
1

AverageSequenceOfNumbersFromParamArray = _
varSumOfNums / varItemCount

End Function

Function AverageSequenceOfNumbersPlusOneFromParamArray( _
SequenceOfNumbers As Variant _
) As Double
Dim varSequencePlusOne As Variant
Dim lngLBound As Long
Dim lngUBound As Long

varSequencePlusOne = SequenceOfNumbers
lngLBound = LBound(varSequencePlusOne)
lngUBound = UBound(varSequencePlusOne)

ReDim Preserve varSequencePlusOne(lngLBound To lngUBound + 1)
varSequencePlusOne(lngUBound) = 99

AverageSequenceOfNumbersPlusOneFromParamArray = _
AverageSequenceOfNumbersFromParamArray(varSequence PlusOne)

End Function

My results from the Immediate window were as follows:

? AverageSequenceOfNumbersFromParamArray(1,2,3,4,5)
3
?
AverageSequenceOfNumbersFromParamArray(Array(1,2,3 ,4,5),6,7,8,Array(9,10,11,12,13))
7
?
AverageSequenceOfNumbersPlusOneFromParamArray(Arra y(1,2,3,4,5,6,7,8,9,10,11,12,13))
12.6428571428571

Thanks for all your help! If there are any traps that I should be aware
of by taking this approach then please let me know.

Edlueze.

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: andy | last post by:
i am coding a dll which contains a function take a const wchar_t* as parameter, call it form a exe, the wchar_t is passed to dll function, but the function in dll will then call another function...
1
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
3
by: Panda2 | last post by:
Say I have a structure such as: struct Barn{ char type; int number; }animal so for example we might have data like animal.type cow animal.number 10
4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
2
by: Ian Partridge | last post by:
Hi, I want to write a varargs function which then passes its parameters to another varargs function. I have RTFFAQ: http://www.eskimo.com/~scs/C-faq/q15.12.html which says that the other...
3
by: junk | last post by:
Hi, Given the following function:- void foo (char *fmt, ...) { } I know how to use va_start, va_end etc to process the parameters but is there an easy way of passing the parameter list...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.