Ad Space — Top Banner

E2018

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

E2018 means you provided a non-integer value in a place that requires an integer, most often an array index. Delphi array indexes must be integer types — floating point, string, or Boolean values are not allowed. Fix it by converting the value to an integer type using Round(), Trunc(), or an explicit cast.

Affected Models

  • Delphi 10.4 Sydney
  • Delphi 11 Alexandria
  • Delphi 12 Athens
  • Embarcadero RAD Studio
  • Free Pascal / Lazarus

Common Causes

  • Using a floating-point variable such as Double or Single as an array index
  • Using a string value as an array subscript instead of an integer
  • Passing a Boolean expression where an integer index is expected
  • Using a non-integer loop variable in a for loop or array index context
  • Calling a function that returns Double and passing it directly to an array index

How to Fix It

  1. Locate the line the IDE highlights and identify which value is not an integer.

    Common culprits are variables of type Double, Single, Extended, String, or Boolean used as array indexes.

  2. If you have a floating-point value, convert it with Round() or Trunc() before using it as an index.

    Round(3.7) gives 4 (rounds to nearest). Trunc(3.7) gives 3 (truncates toward zero). Choose based on your logic.

  3. If you have a string that represents a number, convert it first: MyArray[StrToInt(MyString)].

    StrToInt raises an exception if the string is not a valid integer. Use StrToIntDef for a safe default.

  4. If the value comes from a function returning Double, assign it to an intermediate Integer variable after converting.

    Declare Index: Integer; then Index := Round(CalculateIndex()); then use MyArray[Index].

  5. Check for loop variables — Delphi for loops require an ordinal (integer or enumeration) type as the counter.

    If you need a floating-point loop, use a while loop: X := Start; while X <= Stop do begin ... X := X + Step; end;

When to Call a Professional

This is a compiler error — no professional repair needed. Fix it by reading the error message and correcting the code. The Delphi IDE highlights the exact line causing the problem.

Frequently Asked Questions

Why can Delphi not use a float as an array index?

Array indexes in Delphi are ordinal values — they must be exact, discrete positions. A float like 2.7 is ambiguous as an index: should it mean element 2 or 3? Delphi requires you to make that decision explicitly by rounding or truncating before indexing.

What integer types are valid as array indexes in Delphi?

Any ordinal type is valid: Integer, Cardinal, Byte, Word, LongInt, Int64, and enumeration types. Char and Boolean are also ordinal types and can technically be array indexes, though they are rarely used that way. Double, Single, Extended, and String are not ordinal and will always cause E2018.

What is the difference between Round and Trunc when converting a float to an index?

Round rounds to the nearest integer — 2.5 becomes 3, 2.4 becomes 2. Trunc drops the fractional part toward zero — 2.9 becomes 2, -2.9 becomes -2. Choose Round when you want the closest position, and Trunc when you want the floor (or ceiling for negatives).