E2034
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
E2034 means Delphi cannot automatically convert one type to another in the assignment or expression you wrote. Unlike some languages, Delphi does not silently convert between incompatible types — you must do it explicitly. Fix it by using the correct conversion function or typecast for the types involved.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- Assigning an Integer to a String variable without calling IntToStr()
- Assigning a floating-point value to an Integer variable without Round() or Trunc()
- Passing a String where a PChar or PAnsiChar is expected
- Assigning a class instance to a variable of a different, unrelated class type
- Mixing AnsiString and UnicodeString without an explicit conversion
How to Fix It
-
Read the full error message — it tells you the source type (X) and target type (Y), such as 'Cannot convert Integer to String'.
Knowing both types immediately tells you which conversion function to use.
-
For Integer to String, use IntToStr(): Label1.Caption := IntToStr(MyInteger);
The reverse (String to Integer) uses StrToInt() or the safe StrToIntDef() which provides a default if conversion fails.
-
For Float to Integer, use Round() or Trunc() depending on whether you want rounding or truncation.
For Integer to Float, Delphi usually handles the widening automatically, but you can always write 1.0 * MyInteger to be explicit.
-
For String to PChar, use PChar(MyString) or pass it to a function that accepts a PChar parameter directly — Delphi handles this automatically for const PChar parameters.
For AnsiString versus UnicodeString, use AnsiString(MyString) or string(MyAnsiString) for explicit conversion.
-
For incompatible class types, check whether you actually need a typecast (using as) or whether there is a design issue where the wrong type is being used.
Use MyObject as TTargetClass only when you are certain the object really is that type at runtime. Use Assigned() and is to check first.
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 does Delphi not convert types automatically like some other languages do?
Delphi's strict type system is intentional — automatic conversions often cause subtle bugs like data loss or precision errors. By requiring explicit conversions, Delphi forces you to think about and accept the consequences of the conversion. This makes code more predictable and easier to audit.
What is the difference between a typecast and a conversion in Delphi?
A typecast (like Integer(MyByte)) reinterprets the existing bits in memory as a different type — no new value is calculated. A conversion (like IntToStr(42)) creates a new value of the target type from the original value. For numeric types, typecasts and conversions often give the same result, but for strings and objects they are very different.
Can E2034 happen with compatible numeric types like Integer and Cardinal?
Yes — even between seemingly similar types, Delphi may report E2034 if the assignment could lose information or is ambiguous. For example, assigning an Int64 to an Integer without a cast will cause an error because Int64 can hold values that do not fit in Integer. Always cast explicitly when assigning between numeric types of different sizes or signedness.