Class Operators (is & as) in Delphi - ablealias/Delphi GitHub Wiki

The is Operator

The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an object. The expression: object is class returns True if object is an instance of the class denoted by class or one of its descendants, and False otherwise. (If object is nil, the result is False.) example,

if ActiveControl is TEdit then TEdit(ActiveControl).SelectAll;

This statement casts the ActiveControl variable to the TEdit type. First it verifies that the object referenced by ActiveControl is an instance of TEdit or one of its descendants.

The as Operator

The as operator performs checked typecasts. The expression

object as class

returns a reference to the same object as object, but with the type given by class. At run time, object must be an instance of the class denoted by class or one of its descendants, or be nil; otherwise an exception is raised. If the declared type of object is unrelated to class - that is, if the types are distinct and one is not an ancestor of the other - a compilation error results. For example:

with Sender as TButton do
  begin
   Caption := '&Ok';
   OnClick := OkClick;
  end;

The rules of operator precedence often require as typecasts to be enclosed in parentheses. For example:

(Sender as TButton).Caption := '&Ok';