Delphi – E2055 Illegal type in Read/Readln statement

When you try to read a variable that isn’t of a legal type in a Read or Readln, you’ll get this error in your delphi program.

Check the variable’s type and make sure you don’t have any dereferencing, indexing, or field selection operators missing.

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

Variables of enumerated types cannot be read 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.

Reading a string and looking it up in an auxiliary table is the solution. We didn’t bother with error checking in the previous example, so any string will be considered as ‘blue.’ In fact, we’d most likely display an error message and request that the user try again.