• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Pass function as parameter

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
michaelmadsen
Asked:
michaelmadsen
1 Solution
 
markhoyCommented:
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
 
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
The new generation of project management tools

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.

 
fritz_the_blankCommented:
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
 
JNSTAUBCommented:
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)
0
 
fritz_the_blankCommented:
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
 
fritz_the_blankCommented:
Were any of these comments helpful? If so, please select one as an answer to close out the question.

Fritz the Blank
0
 
michaelmadsenAuthor Commented:
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
 
fritz_the_blankCommented:
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
 
michaelmadsenAuthor Commented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now