I would very much like to be able to pass a function or sub as a parameter to another function. I am thinking about something like:
Dim a, b, result
Function Add()
result = a + b
End Function
Function Multiply()
result = a * b
End Function
Function Math(byVal firstarg, byVal secondarg, byRef mathFunction)
a = firstarg
b = secondarg
Call mathFunction
Math = result
End Function
addresult = Math (5,5, Add())
multiplyresult = Math (5,5, Multiply())
The above probably doesn't work. My question is if something like this is possible. To transfer a function as a parameter argument to another function.
I only have VBScript to do this (no JScript)
Function Multiply(a,b)
Multiply = a * b
End Function
a=10
b=15
addresult = Math (5,5, Add(a,b))
multiplyresult = Math (5,5, Multiply(a,b))
0
michaelmadsenAuthor Commented:
markhoy:
I don't think you have got the question: it's about how to pass a function as parameter
0
michaelmadsenAuthor Commented:
Ok, I figured it out myself:
Function Math(byVal firstarg, byVal secondarg, byRef mathFunction)
Execute mathFunction
End Function
Math 1,1, "Add"
0
With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
And it is the wierdest thing too, isn't it? For example, I wanted to created an abstracted function for closing recordsets:
sub ClearRecordSet(ByRef strRecordSetName)
if IsObject(strRecordSetName) then
if not strRecordSetName is Nothing Then
if strRecordSetName.state <> 0 then
strRecordSetName.close
end if
set strRecordSetName = Nothing
end if
end if
end sub
In order to do that, I had to use the ByRef in the parameter. However, all of the VBScript documentation that I read said that ByRef was the default.
asimple way is to pass the math routine as string and perform a select case
case "add"
result=add(a,b)
.....
end if
or to create a math object with methods (add,sub,....)
regards
0
michaelmadsenAuthor Commented:
fritz_the_blank:
actually the "byRef mathFunction" has nothing to do with the solution in this case. It could as well have been saying "byVal mathFunction" as I am transferring a string.
But the conclusion is the same: VB sucks! ;-)
0
michaelmadsenAuthor Commented:
JNSTAUB:
Maybe you noticed that I allready answered this question myself.
Anyway, I wasn't looking for a work-around for this. I was looking for a way to pass a function (as you can do it in "normal" languages like c or java)
Be sure to close out this question by accepting one of the comments as an answer, but probably more appropriately in this case, by posting a request for deletion and point refund in the Community Support topic area.
Function Add(a,b)
Add = a + b
End Function
Function Multiply(a,b)
Multiply = a * b
End Function
a=10
b=15
addresult = Math (5,5, Add(a,b))
multiplyresult = Math (5,5, Multiply(a,b))