Delphi Error – E2026 Constant expression expected

Delphi Compiler Error

E2026 Constant expression expected

Reason for the Error & Solution

The compiler expected a constant expression here, but the expression it found turned out not to be constant.

program Produce;
const
  Message = 'Hello World!';
  WPosition = Pos('W', Message);
begin
end.

The call to Pos is not a constant expression to the compiler, even though its arguments are constants, and it could in principle be evaluated at compile time.

program Solve;
const
  Message = 'Hello World!';
  WPosition = 7;
begin
end.

So in this case, we just have to calculate the right value for WPosition ourselves.

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