Partager via


The return value of a method can be intercepted using BindEvent

Here’s a blog post that I wrote a long time ago, but forgot to publish.

Marco Cenzato commented on Why doesn't my form close?

Here is another case; what do you think: is related?

** Comment the define and rerun the program

#Define USE_BINDEVENT

Activate Screen

Clear

Local oForm

oForm = NewObject('MyForm')

If Vartype(oForm) == 'O'

      ? "A form was created: this is not the expected behaviour"

      oForm.Show(1)

Else

      ? "No form was created: this is the expected behaviour"

EndIf

Define Class MyForm As Form

      Autocenter = .T.

      #Ifdef USE_BINDEVENT

      Procedure Load

            BindEvent(This, 'Init', This, 'InitCompleted', 1)

      EndProc

      Procedure InitCompleted

            ? 'InitCompleted fired'

      EndProc

      #EndIf

      Procedure Init

            Return .F.

      EndProc

EndDefine

After looking at the code, I see that the Init method is returning false, meaning that the object should not be created. Normally, VFP calls the Init event, sees the false, and fails the object creation. However, the BindEvent is being used to bind to the Init event, meaning that the Return value is lost. See What to do with the Bindevent return value?

A Method is some code that gets executed when called. An Event is some code that gets executed in response to some event, such as a mouse move or keystroke. The code in an event can also be called just like a method. In this sample, the Init method is being treated like an event by BindEvent, and so its return value is lost.

Comments

  • Anonymous
    October 22, 2006
    Thank you Clavin for your response :-) but, sorry, I'm not undarstanding :-( In your previous post "What to do with the Bindevent return value?" you wrote: "Event handlers handle events. They don’t have return values (...)." and "... 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." Something is not clear to me: may be the chages apply only when BindEvent() binds to a method and not to an Event (like Init, Keypress, MouseMove...) ? Also in this post you write: "... BindEvent is being used to bind to the Init event, meaning that the Return value is lost." It is lost because overwritten by the last callede delegate return value, I'm right?