Vb6 - How to pass a function to a parameter?

How do I do to pass a function by a parameter that is later called in VB6? there will be something like what I need, maybe any of these options:

Private Sub Command1_Click() Call callMethod(MyPrivateFunction) Call callMethod(MyPublicFunction) Call callMethod(MyPrivateSub) Call callMethod(MyPublicSub) End Sub Private Function MyPrivateFunction() MsgBox "MyPrivateFunction" End Function Public Function MyPublicFunction() MsgBox "MyPublicFunction" End Function Private Sub MyPrivateSub() MsgBox "MyPrivateSub" End Sub Public Sub MyPublicSub() MsgBox "MyPublicSub" End Sub Public Function callMethod(ByRef func As Object) Call func End Function 
+7
vb6
source share
3 answers

IIRC there is a “AddressOf” function in VB6 for getting function addresses, but you are likely to encounter a lot of problems using this function address from VB6.

The SOP method for this is "CallByName ()", which allows you, you know, call functions, etc. by their names.

Finally, you can also take the high road using the standard OO solution for this: instead of passing a function, write your own class that implements the special interface of your own design, “MyFunctionInterface”. This interface has only one "FunctionToCall (..)" method, which you can implement in different classes to call the various functions that you need. Then you pass an instance of one of these classes to your routine, which receives it as "MyFunctonInterface" and calls the "FunctionToCall" method on it. Of course, this requires many small design changes ...

+6
source share

You cannot pass a function, but you can pass an object that behaves like a function (sometimes called a "functor"). I use it all the time. If you use the "functor" class. Implements an interface, the call will be type safe. For example:

Abstract class (Interface) IAction.cls:

 Option Explicit Public Sub Create(ByVal vInitArgs As Variant) End Sub Public Function exe() As Variant End Function 

Functor displaying the URL in the default browser:

 Option Explicit Implements IAction Dim m_sUrl As String Public Sub IAction_Create(ByVal vInitArgs As Variant) m_sUrl = vInitArgs End Sub Public Function IAction_exe() As Variant Call RunUrl(m_sUrl) 'this function is defined elsewhere Exit Function 

Now you can create a bunch of these classes, save them in a collection, pass them to any function or method that expects IAction, etc.

+3
source share

Your comment that you use Microsoft.XMLHTTP OnReadyStateChange is interesting. The MSDN page for OnReadyStateChange says it is "designed for use in scripting environments and not available in Microsoft® Visual Basic®."

The same page says that in Visual Basic 6 you have to do this. Put this variable declaration at module level

  Dim WithEvents xmldoc As DOMDocument30 

and then you can handle the event in the usual way.

  Private Sub xmldoc_onreadystatechange() ' Deal with the event here ' End Sub 

Alternatively, more discussions about function calls by name to this question . But I think this is the wrong solution for your problem.

+1
source share

All Articles