What to do with the Bindevent return value?
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
Comments
- Anonymous
November 03, 2005
The comment has been removed - Anonymous
January 02, 2006
Looks to me like in VFP 9 method invokation modes 2 and 3 don't work at all with the delegate not firing...
Also in those scenarios where the event is 'skipped over' if possible shouldn't the delegate return the value? - Anonymous
October 20, 2006
Here’s a blog post that I wrote a long time ago, but forgot to publish. Marco Cenzato commented on Why