DEFINE [ PRIVATE | PROTECTED | PUBLIC ] [ STATIC | ABSTRACT ] [ OVERRIDE ]
EVENT event-name signature-spec
|
DEFINE [ PUBLIC ] EVENT event-name signature-spec
|
DEFINE [ PROTECTED | PUBLIC ] [ OVERRIDE ] ABSTRACT
EVENT event-name signature-spec
|
[ PRIVATE
| PROTECTED
| PUBLIC
]
Defines an event that is a static member of the class type for which it is defined and that is scoped to the ABL session where it is referenced. ABL creates one copy of the specified class static event on first reference to the class type, and ABL creates only one such copy for any number of instances of the class that you create. You can subscribe a handler for an accessible static event in any piece of code. You can publish a static event only in an instance or static method that is defined within the same class definition where the static event is defined.
Without this option, ABL defines an instance event that is scoped to a single instance of the class where it is defined. ABL creates one copy of the specified instance event for each such class instance that you create. You can subscribe a handler for any public instance event (abstract or non-abstract) in any procedure, or in any instance or static method defined inside or outside of the class where the instance event is defined. Any static method can publish the public instance event only using an object reference to a class instance that defines the event as a member. If the referencing static method is defined in the same class as the public instance event, the class must instantiate itself in order to have access to an instance reference.
You can publish a non-abstract instance event only in a method that is defined within the same class definition where the instance event is defined. If the method is static, the instance event must also be public and you can only publish it using an object reference to an instance of the class. An instance method defined in the same class can also publish a public instance event using an object reference to an instance of the class.
Note:
|
You cannot use a class instance that is not equal to the THIS-OBJECT system reference to subscribe a handler for a private or protected instance event, or to publish a private or protected instance event that is defined in the same class, because PRIVATE and PROTECTED access modes are instance based in ABL. Thus, private and protected instance members are accessible only to other members of the same class instance, where as public instance members can be accessed from other instances of the same class, including the session “static instance” of the class.
|
When you specify OVERRIDE, event-name must be identical to the name of the overridden abstract event, and
signature-spec must specify a signature that is identical to the signature defined for the overridden event. In addition, the access mode must not be more restrictive than the access mode defined for the overridden event. When overriding a .NET event, the
signature-spec must specify the same delegate that is defined for the .NET event. For more information, see the description of the
signature-spec option.
If you specify the ABSTRACT option, your overriding event is also defined as abstract, and it must be implemented in a class derived from the defining class. Note that you do not have to override an inherited abstract event that you want to remain abstract as long as the inheriting class is also abstract. However, doing so allows you to specify a less restrictive access mode for the abstract event.
Note:
|
Members of a class are grouped into six namespaces, including buffers/temp-tables, methods, variables/properties/events, ProDataSets, queries, and data-sources. Variables, properties, and events defined as members of a class share the same namespace. There can be only one class member in this namespace with a given name (not counting abstract member overrides).
|
{ [ SIGNATURE ] VOID ( [ parameter [ , parameter ] ... ] )
| [ DELEGATE ] [ CLASS ] dotNet-delegate-type }
|
[ SIGNATURE
] VOID (
[ parameter [ ,
parameter ] ... ] )
If this event implements an ABL interface event or overrides an inherited ABL abstract event (using the OVERRIDE option), the interface or inherited abstract event must also be defined with an ABL method signature that matches this signature. If the ABL interface or inherited ABL abstract event is defined with a .NET signature (
dotNet-delegate-type), you must use the .NET signature option to define this event instead.
[ DELEGATE
] [ CLASS
] dotNet-delegate-type
Defines a .NET signature for the event as specified by a .NET delegate type (
dotNet-delegate-type), for example,
System.EventHandler. You can specify the delegate class name without a namespace (for example,
EventHandler) with the presence of an appropriate
USING statement. The DELEGATE or CLASS keyword is optional for readability. However the CLASS keyword, only, also disambiguates a delegate type name that might be identical to an ABL primitive type name. Note that ABL only supports .NET delegate types that conform to the .NET Framework convention for event handlers. This convention defines an event handler signature with a VOID return type and two input parameters, where the first parameter is a
System.Object and the second parameter is a
System.EventArgs or a .NET class derived from
System.EventArgs. The .NET delegate type can also be a constructed generic type. For more information on .NET generic types, see the
Data types reference entry.
You can use this option to define the signature for an ABL class event even if the event does not implement or override a .NET event. However, if this event implements an ABL interface event or overrides (using the OVERRIDE option) an inherited ABL abstract event that is, itself, defined with a .NET delegate, you must also define the signature for this event using an identical
dotNet-delegate-type.
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.
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.