Re: BindEvent and RaiseEvent

Author: Grigore Dolghin

Posted: 2005-10-25 at 15:17:26

Hi

Here's a piece of code I have put together today, which demonstrates very

clear (IMO) the difference.

*------- We save the code below in test.prg:

*------ First listener

Define Class Obj1 As custom

Procedure Init

BindEvent(MyCustomClass,"CustomEvent",This,"HandleEvent")

EndProc

Procedure HandleEvent

Wait Window This.Name

EndProc

EndDefine

*------ Second listener

Define Class Obj2 As Custom

Procedure Init

BindEvent(MyCustomClass,"CustomEvent",This,"HandleEvent")

EndProc

Procedure HandleEvent

Wait Window This.Name

EndProc

EndDefine

*------ "Listened" object.

Define Class MyCustomClass As Custom

Procedure CustomEvent

EndProc

EndDefine

*-------------------EOF

Now we run the following, line by line, in command window:

Set Procedure To test.prg

MyCustomClass = CreateObject("MyCustomClass")

Obj1 = CreateObject("Obj1")

Obj2 = CreateObject("Obj2")

*--- at this moment the objects are created and bound to listened object.

MyCustomClass.CustomEvent() && triggers two wait windows

RaiseEvent(MyCustomClass,"CustomEvent") && triggers two wait windows.

*------ IOW there is NO difference between RaiseEvent and calling that

method

Now we change the first class definition in the following way:

*------------

Define Class Obj1 As custom

Procedure Init

BindEvent(MyCustomClass,"CustomEvent",This,"HandleEvent",2)

EndProc

Procedure HandleEvent

Wait Window This.Name

EndProc

EndDefine

*-------------EOF

Notice 2 at the end of BindEvent line. We'll run these lines in Command

Window:

Set Procedure To numeprg.prg

MyCustomClass = CreateObject("MyCustomClass")

Obj1 = CreateObject("Obj1")

Obj2 = CreateObject("Obj2")

*--- at this moment the objects are created and bound to listened object.

MyCustomClass.CustomEvent() && only ONE wait window is generated, from Obj2

RaiseEvent(MyCustomClass, "MyCustomEvent") && both wait windows show up.

In a single phrase: BindEvent allows to specify a method which gets executed

whenever some other method is executed. If we add the fifth parameter to

BindEvent, namely 2 or 3, we can force BindEvent to react to EVENT only

(raised by RaiseEvent), and not to METHOD (called in normal way).

Thanks :)

Grigore Dolghin.

----- Original Message -----

From: "Sietse Wijnker" <sietse.wijnker@sw-software.nl>

To: <profoxtech@leafe.com>

Sent: Tuesday, October 25, 2005 1:55 AM

Subject: RE: BindEvent and RaiseEvent

> RaiseEvent and Bindevent are complementary.

> 1st you can bind 'At Runtime' any custom method to an existing event using

> Bindevents:

> BindEvent(_Screen, "resize", goResizeHandler, "handleResize")

> Then to trigger the event from code you dont call Screen.Resize() but

> write

> RaiseEvent(Screen, "Resize")

> Using the 5th param (value=2 in VFP9) for bindevent you can specify if the

> delegate method (goResizeHandler.handleResize) will react to

> Screen.Resize()

> AND RaiseEvent(Screen, "Resize") AND system events or only to

> RaiseEvent(Screen, "Resize") AND system events

>

> I intentionally used the _Screen.resize event in this example because in's

> hard to write any code in the event.... (but it can be done, but now it's

> no

> issue)

>

> Regards,

> Sietse Wijnker

>

>

>> -----Original Message-----

>> From: profoxtech-bounces@leafe.com

>> [mailto:profoxtech-bounces@leafe.com] On Behalf Of Grigore Dolghin

>> Sent: maandag 24 oktober 2005 22:26

>> To: profoxtech@leafe.com

>> Subject: BindEvent and RaiseEvent

>>

>> Hello, everyone

>>

>> I think I am missing the phylosophy here. What's the

>> difference between these two? As far as I can understand, I

>> can raise an event from any method; in other words,

>> RaiseEvent can cause a custom method to behave like an event

>> - to get executed when something happens. Well; let's assume

>> in a button Click() snippet I enter a RaiseEvent() and raise

>> a custom method as event. The same can be achieved by binding

>> through BindEvent my custom method and button's Click().

>> Also, I can simply call my custom method from button Click().

>>

>> What's the difference? what can be done thru RaiseEvent and

>> cannot be done thru BindEvent()? a sample would be greatly

>> appreciated. I don't understand what I am missing, although I

>> am obviusly missing something.

>>

>> Thank you.

>>

>>

>>

[excessive quoting removed by server]

©2005 Grigore Dolghin