?
Solved

Pass function as parameter

Posted on 2003-03-25
12
Medium Priority
?
320 Views
Last Modified: 2007-12-19
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

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)
0
Comment
Question by:michaelmadsen
[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
  • Learn & ask questions
12 Comments
 
LVL 7

Expert Comment

by:markhoy
ID: 8201366
Using your example, how about something like:

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

0
 

Author Comment

by:michaelmadsen
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

by:michaelmadsen
ID: 8201383
Ok, I figured it out myself:

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

Math 1,1, "Add"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:fritz_the_blank
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

by:JNSTAUB
ID: 8204122
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
 

Author Comment

by:michaelmadsen
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

by:michaelmadsen
ID: 8204970
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)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
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

by:fritz_the_blank
ID: 8319777
Were any of these comments helpful? If so, please select one as an answer to close out the question.

Fritz the Blank
0
 

Author Comment

by:michaelmadsen
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

by:
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

by:michaelmadsen
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question