Delphi Error – E2089 Invalid typecast

Delphi Compiler Error

E2089 Invalid typecast

Reason for the Error & Solution

This error message is issued for type casts not allowed by the rules. The following kinds of casts are allowed:

– Ordinal or pointer type to another ordinal or pointer type

– A character, string, array of character or pchar to a string

– An ordinal, real, string or variant to a variant

– A variant to an ordinal, real, string or variant

– A variable reference to any type of the same size.

Note that casting real types to integer can be performed with the standard functions Trunc and Round.

There are other transfer functions like Ord and Chr that might make your intention clearer.

program Produce;

begin
  Writeln( Integer(Pi) );
end.

This programmer thought he could cast a floating point constant to Integer, like in C.

program Solve;

begin
  Writeln( Trunc(Pi) );
end.

In the Delphi language, we have separate Transfer functions to convert floating point values to integer.