9.8.7 Class operators

Class operators are slightly different from the operators above in the sense that they can only be used in class expressions which return a class. There are only 2 class operators, as can be seen in table (9.8).

Table 9.8: Class operators


is Checks class type
as Conditional typecast

An expression containing the is operator results in a boolean type. The is operator can only be used with a class reference or a class instance. The usage of this operator is as follows:

 Object is Class

This expression is completely equivalent to


If Object is Nil, False will be returned.

The following are examples:

  A : TObject;  
  B : TClass;  
  if A is TComponent then ;  
  If A is B then;  

The as operator performs a conditional typecast. It results in an expression that has the type of the class:

  Object as Class

This is equivalent to the following statements:

  If Object=Nil then  
  else if Object is Class then  
    Raise Exception.Create(SErrInvalidTypeCast);

Note that if the object is nil, the as operator does not generate an exception.

The following are some examples of the use of the as operator:

  C : TComponent;  
  O : TObject;  
  (C as TEdit).Text:=’Some text’;  
  C:=O as TComponent;