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.