Quick start manual

7-26
Delphi Language Guide
Class references
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.) 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,
if ActiveControl is TEdit then TEdit(ActiveControl).SelectAll;
This statement casts a variable to TEdit after first verifying that the object it references
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
runtime, 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';
Class methods
A class method is a method (other than a constructor) that operates on classes instead
of objects. The definition of a class method must begin with the reserved word class.
For example,
type
TFigure = class
public
class function Supports(Operation: string): Boolean; virtual;
class procedure GetInfo(var Info: TFigureInfo); virtual;
ƒ
end;