PreviousNextIndex

DEFINE EVENT statement

Defines an ABL class event, declares a class event prototype in an ABL interface, or overrides an abstract class event inherited from an ABL or .NET abstract super class. A class event is a member of the class in which it is defined. You can publish a non-abstract event from within the defining class definition using the built-in Publish( ) event method in response to a condition that you determine. You can subscribe a class method or internal procedure as a handler for any accessible event (abstract or non-abstract) using the built-in Subscribe( ) event method. This handler executes whenever the event is published. You can also unsubscribe any handler using the built-in Unsubscribe( ) event method. The following description begins with general syntax for defining a class event.

Note: This statement is applicable only when used in a class or interface definition (.cls) file. For more information on class events, see the "Class Events Reference" section.
Syntax

DEFINE [ PRIVATE | PROTECTED | PUBLIC ] [ STATIC | ABSTRACT ] [ OVERRIDE ] 
  EVENT event-name signature-spec  

Use the following syntax to declare a class event prototype in an interface:

DEFINE [ PUBLIC ] EVENT event-name signature-spec 

Use the following syntax to declare an abstract class event prototype:

DEFINE [ PROTECTED | PUBLIC ] [ OVERRIDE ] ABSTRACT  
  EVENT event-name signature-spec 

[ PRIVATE | PROTECTED | PUBLIC ]
[ STATIC ]
[ ABSTRACT ]
[ OVERRIDE ]
EVENT event-name
signature-spec
[ SIGNATURE ] VOID ( [ parameter [ , parameter ] ... ] )
[ DELEGATE ] [ CLASS ] dotNet-delegate-type
Examples

The following sample class and procedure files define, publish, subscribe an event handler to, and unsubscribe an event handler from an ABL class event. The r-EventPublish sample class defines the public NewCustomer event and a PubNewCustomer( ) method to publish it.

r-EventPublish.cls
CLASS r-EventPublish:  
   
  /* Define an event */ 
  DEFINE PUBLIC EVENT NewCustomer  
    SIGNATURE VOID ( INPUT pcCustName AS CHARACTER ). 
       
  /* Code that publishes the event. */ 
  METHOD PUBLIC VOID PubNewCustomer( ): 
     
    DEFINE VARIABLE cCustName AS CHARACTER INITIAL "A Customer Name" NO-UNDO. 
                     
    NewCustomer:Publish( INPUT cCustName ) NO-ERROR. 
     
  END METHOD.  
END CLASS. 

The r-EventSubscribe sample class defines and subscribes a NewCustomer( ) method as a handler for the event when it is instantiated, based on the object reference to the r-EventPublish class that is passed to the constructor. Note that the event handler also unsubscribes itself to the event after it executes.

r-EventSubscribe.cls
CLASS r-EventSubscribe: 
   
  DEFINE VARIABLE rPubObj AS CLASS r-EventPublish NO-UNDO. 
     
  CONSTRUCTOR PUBLIC r-EventSubscribe  
    ( INPUT prPubObj AS CLASS r-EventPublish):  
        
    ASSIGN rPubObj = prPubObj. 
    rPubObj:NewCustomer:Subscribe( NewCustomer_Handler ) NO-ERROR. 
     
  END CONSTRUCTOR. 
  /* Method used as event handler */ 
  METHOD PUBLIC VOID NewCustomer_Handler ( INPUT pcCustName AS CHARACTER ): 
     
    MESSAGE "Subscriber received event NewCustomer" SKIP 
            "CustName =" pcCustName VIEW-AS ALERT-BOX. 
    rPubObj:NewCustomer:Unsubscribe( NewCustomer_Handler ) NO-ERROR. 
  END METHOD.  
END CLASS. 

The r-EventPubSub.p sample procedure instantiates these classes. The procedure then displays a frame that includes a bNewCust button that when clicked runs an internal procedure, which in turn invokes the PubNewCustomer( ) method on the r-EventPublish object to publish the event. This demonstrates how the class defining an event can allow a client class or procedure to publish it by providing a public method for the purpose. Note that any subsequent attempt to click the button does not run the event handler, because the handler has unsubscribed itself.

r-EventPubSub.p
DEFINE VARIABLE rPubObj AS CLASS r-EventPublish NO-UNDO. 
DEFINE VARIABLE rSubObj AS CLASS r-EventSubScribe NO-UNDO. 
DEFINE BUTTON bNewCust LABEL "New Customer". 
DEFINE BUTTON bQuit    LABEL "Quit". 
FORM bNewCust bQuit WITH FRAME aFrame. 
ON CHOOSE OF bNewCust RUN CallNewCust NO-ERROR. 
rPubObj = NEW r-EventPublish( ). 
rSubObj = NEW r-EventSubScribe( rPubObj ). 
ENABLE ALL WITH FRAME aFrame. 
WAIT-FOR CHOOSE OF bQuit OR WINDOW-CLOSE OF CURRENT-WINDOW. 
PROCEDURE CallNewCust: 
   
  /* Call to publish */ 
  rPubObj:PubNewCustomer( ) NO-ERROR. 
END PROCEDURE. 

For more examples of class event definitions, including static and abstract events, see the descriptions of r-CustObj.cls, r-CustObjStatic.cls, and r-CustObjAbstract.cls in the CLASS statement reference entry.

Notes
See also

CLASS statement, INTERFACE statement, Parameter definition syntax, Publish( ) event method, Subscribe( ) event method, Type-name syntax, Unsubscribe( ) event method, USING statement


OpenEdge Release 10.2B
Copyright © 2009 Progress Software Corporation
PreviousNextIndex