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 X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...