views:

639

answers:

2

There is no special type for functions, therefore it is hard for me to see, how to add functions as arguments to functions in excel vba.

What I try to accomplish is something like this:

function f(g as function, x as string) as string
        f = g(x)
end function

Concerning the motivation: I have a masse of little functions all repeating itself but one call to a specific function.

Thanks for taking the time to answer this question!

+2  A: 

From your code, function g takes a string parameter and returns a string. I suggest you create a class module called IStringFunction to act as the definition of an interface that all functions will support, thus:

Class Module IStringFunction

Public Function Evaluate(ByVal s As String) As String
End Function

Then, create a couple of example functions implementing this interface:

Class Module HelloStringFunction

Implements IStringFunction

Public Function IStringFunction_Evaluate(ByVal s As String) As String
    IStringFunction_Evaluate = "hello " & s
End Function

Class Module GoodbyeStringFunction

Implements IStringFunction

Public Function IStringFunction_Evaluate(ByVal s As String) As String
    IStringFunction_Evaluate = "goodbye " & s
End Function

...and finally, some test code to exercise the functions:

(Standard) Module Test

Sub Test()

    Dim oHello As New HelloStringFunction
    Dim oGoodbye As New GoodbyeStringFunction

    MsgBox Evaluate(oHello, "gary")
    MsgBox Evaluate(oGoodbye, "gary")

End Sub

Private Function Evaluate(ByVal f As IStringFunction, ByVal arg As String) As String
    Evaluate = f.Evaluate(arg)
End Function

Note that the class implementing the interface must have methods named <Interface>_<Method> as in the example above, not just <Method> as you'd expect.

Gary McGill
That does not really help him, because he still has to write a seperate evaluation function for each class.
Treb
@Treb: maybe I don't understand the question, but I don't see what you mean. Each class represents a different function, so of course it has to have a separate evaluation function! Maybe if the problem could be re-stated it would be clearer.
Gary McGill
Hi, I think this is the right answer. This is the way to do it in oo-languages, I just didn't know, that interfaces exist in VBA -- my fault. I remember seeing this technic the 1st time in *a little java*. But I have to test the proposed solution. At the moment I got an error message about not implementing the interface.Public Function apply(ByVal tmp As Variant) As BooleanEnd Function---Implements IBooleanFunctionPublic Function apply(ByVal tmp As Variant) As Boolean apply = Application.IsText(tmp)End Function
Roman Glass
Roman, you need to prefix the function name with the name of the interface it implements, so if the interface is called IFoo and the method is called Bar, then in your implementation class the method must be called IFoo_Bar.
Gary McGill
You are right, thank you very much!
Roman Glass
A: 

In Mr. McGill's code, I replaced the word 'evaluate' with 'gibberjabber' in the code module and the class modules because 'evaluate' is already a method in VB and it was confusing me. And it still works. I thought maybe in a few places the word pointed to the new definition and in a few other places it was pointing to the built-in method. I thought wrong. I'm still trying to grok how this is useful. I suspect it's a major help but it hasn't dawned on me yet. That's a plea for help, by the way!

klausnrooster