An assignment statement assigns a new value to a variable, a property, or an event.
assigment-statement:
variable-assignment
event-assignment
event-assigment:
event := expression
event += expression
event -= expression
An assignment statement has two forms: simple assignment and event assignment.
The left part of a simple assignment statement must be an expression, which value is a variable, a property, or an event. The right part of the statement must be an expression of a type that is compatible with the variable type, that is, there should be an implicit conversion from the expression type to the variable type. Otherwise, a compile error occurs.
variable-assignment:
variable := expression
If the left part of the statement is a property, the Set access method should be determined for this property. Otherwise, a compile error occurs.
The statement is executed as follows:
The expression indicated in the statement is calculated and this value is assigned to the variable.
If this variable is a delegate type, the assignment is executed in the same way as in event assignment. Setting the invocation list (:=) for delegate-type variables, unlike for events, is available in any location of the code.
The left part of an event assignment statement must be an expression classified as an event access. The right part of an assignment statement must be an expression that can be implicitly converted to the type of the event, or Null if event processing is to be cancelled.
event-assigment:
event := expression
event += expression
event -= expression
An event assignment statement can have either of the three forms:
Setting the invocation list for a specified event (in the form event := expression) (This form of assignment can be used only in the base type, in which the event is declared).
Extending the invocation list for a specified event (in the form event += expression).
Reducing the invocation list for a specified event (in the form event -= expression).
The operator is executed as follows:
The instance expression of event access is calculated, if there is any.
The value of the expression in the right part of the statement is calculated, and, if required, is cast to the event type using the implicit conversion.
The event invocation list is modified in accordance with the statement form, and the value of the calculated expression in the right part of the statement is used as an operand.
Delegate UserEvent(x, y: integer);
Class TestAssignment
s: string;
Property Text: string
Get
Begin
Return s;
End Get
Set
Begin
//Variable assignment
s := value;
End Set
End Property;
Event OnUserEvent: UserEvent;
Constructor Create(s: string);
Begin
//Property assignment
Self.Text := s;
//Event assignment
OnUserEvent := EventSub1;
OnUserEvent += EventSub2;
End Constructor;
Sub EventSub1(x, y: integer);
Begin
End Sub;
Sub EventSub2(x, y: integer);
Begin
End Sub;
End Class TestAssignment;
See also: