Delphi Error – E2055 Illegal type in Read/Readln statement

Delphi Compiler Error

E2055 Illegal type in Read/Readln statement

Reason for the Error & Solution

This error occurs when you try to read a variable in a Read or Readln that is not of a legal type.

Check the type of the variable and make sure you are not missing a dereferencing, indexing or field selection operator.

program Produce;
type
  TColor = (red,green,blue);
var
  Color : TColor;
begin
  Readln(Color);     (*<-- Error message here*)
end.

We cannot read variables of enumerated types directly.

program Solve;
type
  TColor = (red,green,blue);
var
  Color : TColor;
  InputString: string;
const
  ColorString : array [TColor] of string = ('red', 'green', 'blue');
begin
  Readln(InputString);
  Color := red;
  while (color < blue) and (ColorString[color] <> InputString) do
    Inc(color);
end.

The solution is to read a string, and look up that string in an auxiliary table. In the example above, we didn’t bother to do error checking – any string will be treated as ‘blue’. In practice, we would probably output an error message and ask the user to try again.