Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
BindEvent Gotcha - Fires Twice
Message
General information
Forum:
Visual FoxPro
Category:
Other
Title:
BindEvent Gotcha - Fires Twice
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01158912
Message ID:
01158912
Views:
109
Hi all,
I found a strange gotcha in VFP's native BindEvent. It seems that if you bind an event handler to a property (in order to have the hanlder fire when the property changes), it will fire twice if you switch from storing an object reference in the property to a character value. The first time it fires, the value changes to NULL and the second time it changes to the value that you assigned.

I'm posting this as a reference.
-m@

Example code:
*--An interesting glitch in BindEvents. 
PUBLIC oform1
oform1=NEWOBJECT("form1")
oform1.Show
RETURN

DEFINE CLASS form1 AS form
	Top = 0
	Left = 0
	Height = 220
	Width = 300
	LastValue = ""

	Caption = "BindEvent Test"

	ADD OBJECT Container1 AS container WITH ;
	    TOP = 20, ;
	    LEFT = 80, ;
	    HEIGHT = 60, ;
	    WIDTH = 120, ;
	    NAME = 'Container1'

	ADD OBJECT Command1 AS commandbutton WITH ;
	    TOP = 120, ;
	    LEFT = 20, ;
	    HEIGHT = 27, ;
	    WIDTH = 95, ;
	    NAME = 'Command1', ;
	    CAPTION = "Blank"
		
	ADD OBJECT command2 AS commandbutton WITH ;
		AutoSize = .T., ;
		Top = 120, ;
		Left = 160, ;
		Height = 27, ;
		Width = 95, ;
		Name = "Command2"
	    CAPTION = "Container"

	PROCEDURE Init
		*--Fire the event handler after the value has changed.
		BINDEVENT(this, "LastValue", This, "DisplayLastValueType", 1)  
	ENDPROC

	PROCEDURE command1.Click
		*--Blank out the form property.
		thisform.LastValue = ""
		***
		****Q: Why would the bound event sometimes fire twice after this value change?
	    ****A: When the variable / form property changes from containing a
	    ****an object reference to a character value, VFP first reassigns the value 
	    ****to .NULL. then assigns the new value.
        ****
	ENDPROC

	PROCEDURE command2.Click
	    *--Store an object reference pointing to the container to the form property.
		thisform.LastValue = ThisForm.Container1
	ENDPROC
	
	PROCEDURE DisplayLastValueType
	  IF VARTYPE(ThisForm.LastValue) = "O"
   	    WAIT WINDOW TIMEOUT 2 "LastValue: Changed to Object Reference " + TRANSFORM(SECONDS())
      ELSE
   	    WAIT WINDOW TIMEOUT 2 "LastValue: Changed to blank " + NVL(ThisForm.lastValue,"  [Value is NULL!!!]") + "  " + TRANSFORM(SECONDS())
      ENDIF

ENDDEFINE
Next
Reply
Map
View

Click here to load this message in the networking platform