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.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...