Solved

# Pass function as parameter

Posted on 2003-03-25
Medium Priority
320 Views
Hi everybody

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

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

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)
0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 7

Expert Comment

ID: 8201366

End Function

Function Multiply(a,b)
Multiply = a * b
End Function

a=10
b=15

multiplyresult = Math (5,5, Multiply(a,b))

0

Author Comment

ID: 8201376
markhoy:

I don't think you have got the question: it's about how to pass a function as parameter
0

Author Comment

ID: 8201383
Ok, I figured it out myself:

Function Math(byVal firstarg, byVal secondarg, byRef mathFunction)
Execute mathFunction
End Function

0

LVL 46

Expert Comment

ID: 8203310
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.

Fritz the Blank
0

LVL 4

Expert Comment

ID: 8204122
asimple way is to pass the math routine as string and perform a select case

.....
end if
or to create a math object with methods (add,sub,....)

regards
0

Author Comment

ID: 8204951
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

Author Comment

ID: 8204970
JNSTAUB:

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)
0

LVL 46

Expert Comment

ID: 8205062
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.

Fritz the Blank
0

LVL 46

Expert Comment

ID: 8319777

Fritz the Blank
0

Author Comment

ID: 8319823
Hi fritz_the_blank

No, none of the crap above was useful. Except for my own answer. But since I can't give the points to myself, I just leave it be.

I have no idea how to close this question, and I can't be bothered with stuff like that.
0

LVL 46

Accepted Solution

fritz_the_blank earned 500 total points
ID: 8319844
Well, you have two choices:

One is to request a delete and a point refund by posting the request at:

http://www.experts-exchange.com/Community_Support/

The second is to select one of the above comments as an answer so that the thread will be closed.

Fritz the Blank
0

Author Comment

ID: 8319886
Fritz the Blank:

Well, the supportstuff is just too f...... tiresome, so you'll get the points (even if you didn't answer the question)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is tâ€¦
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was writtenâ€¦
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.â€¦
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
###### Suggested Courses
Course of the Month14 days, 17 hours left to enroll