The BINDEVENT( ) Function allows you to attach code to run when other code is executed. It’s sort of like an event hook.

The first example below has a method called “Foobar” which is called to return a value. After BindEvent is called, the method is called again. The “FoobarHandler” code is executed. Now there are 2 routines that were called when the original “Foobar” method was called, each of which return a value. Which Return value should be used?

 

Event handlers handle events. They don’t have return values (except maybe to say whether the event was indeed handled).

 

The released version of VFP8 used the return value of the delegate. It gets even more complicated when there are multiple routines bound to the original, some executing before and some executing after. Customers reported such issues so that was changed for the released version of VFP9: the Return value of the original method is used.  That way, the behavior is consistent and predictable. Also, adding a BindEvent to code won’t change the original author’s intended return value.

 

oevent = CREATEOBJECT("cEvent")

ohandler = CREATEOBJECT("cHandler")

?"Before bindevent"

? oevent.Foobar()

?

?

?"Bindevent",BINDEVENT(oevent,"Foobar",ohandler,"FoobarHandler",1)

 

?"After bindevent"

 

? oevent.Foobar()

 

DEFINE CLASS cEvent AS custom

      PROCEDURE Foobar

            RETURN PROGRAM()+" Main Return Value"

ENDDEFINE

 

 

DEFINE CLASS cHandler AS custom

      PROCEDURE FoobarHandler

            ?"Here I am in "+PROGRAM()

            ?

            RETURN PROGRAM()+" Handler Return Value"

ENDDEFINE

 

 

 

This code binds 2 methods to the original Foobar:

 

oevent = CREATEOBJECT("cEvent")

ohandler = CREATEOBJECT("cHandler")

?"Before bindevent"

? oevent.Foobar()

?

?

?"Bindevent",BINDEVENT(oevent,"Foobar",ohandler,"FoobarHandler",1)

?"Bindevent",BINDEVENT(oevent,"Foobar",ohandler,"FoobarHandler2",1)

 

?"After bindevent"

 

? oevent.Foobar()

 

DEFINE CLASS cEvent AS custom

      PROCEDURE Foobar

            RETURN PROGRAM()+" Main Return Value"

ENDDEFINE

 

 

DEFINE CLASS cHandler AS custom

      PROCEDURE FoobarHandler

            ?"Here I am in "+PROGRAM()

            ?

            RETURN PROGRAM()+" Handler Return Value"

      PROCEDURE FoobarHandler2

            ?"Here I am in "+PROGRAM()

            ?

            RETURN PROGRAM()+" Handler2 Return Value"

ENDDEFINE