Event Access
Events of a Class, Record or Interface can be accessed simply by specifying their name.
For events of the current type (i.e. the class or record that the current code is also a part of), simply the name of the event on its own suffices to access it. To access events of a different type (or a different instance of the same type), a Member Access Expression is used, appending an .
or :
to the expression that represents the instance, followed by the event name:
lButton.Click += OnClick;
Using self
to Avoid Ambiguity
The self
expression can be used to explicitly access an event on the current instance, in cases where the name of the field is hidden by a different identifier in scope:
Working with Events
Events are multi-cast and hold a chain list of Blocks that is maintained internally. As such, Events are not "read" or "written" in the commom sense, but rather new handlers can be added or removed to an event, using the spcialized +=
and -=
operators:
lButton.Click += OnClick; // add a handler
...
lButton.Click -= OnClick; //remobve it agaun.
Additionally, events can be raised (or triggered, fired), which essentially will call all blocks subscribed to the event, in an undetermined order.
By default, only the class that declares an event can call it; external code can only add or remove handlers. An event is called using the regular Method Call expression, but requires parenthesis to be provided, even if the event is parameterless.
if assigned(OnClick) then
OnClick(args); // foire the Events
See Also
- Events
- Member Access Expressions