Delphi Error – X1012 Constant expression violates subrange bounds

Delphi Compiler Error

X1012 Constant expression violates subrange bounds

Reason for the Error & Solution

This error message occurs when the compiler can determine that a constant is outside the legal range. For example, if you assign a value to an variable that is higher than MaxInt, you trigger this error.

Subranges Example

This can occur for instance if you assign a constant to a variable of subrange type.

The following example code triggers this error message:

program Produce;
var
  Digit: 1..9;
begin
  Digit := 0;
end.

To fix this issue, you must either include the assigned value in the range:

program Solve;
var
  Digit: 0..9;
begin
  Digit := 0;
end.

Alternatively, change the assignment so that the assigned value is within the legal range:

program Solve;
var
  Digit: 1..9;
begin
  Digit := 1;
end.

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...