E2038
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
E2038 means Delphi expected a class type in the given context but found a record, interface, primitive, or other non-class type. This often happens when inheriting from a record instead of a class, or when using class-specific syntax on the wrong type. Fix it by ensuring the type involved is a class (declared with the class keyword) rather than a record or primitive type.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- Trying to inherit from a record type instead of a class type
- Using the 'as' keyword to cast a non-class type
- Calling a constructor (Create) on a type that is not a class
- Declaring an exception class that inherits from a record instead of Exception
- Using class helper syntax on a non-class type
How to Fix It
-
Identify the type named in the error message and check how it was declared — look for its type declaration in the code.
Search for 'type TMyType = ' and check if it says 'class' or 'record' after the equals sign.
-
If you intended to create a class, change 'record' to 'class' in the type declaration.
Classes and records have different memory management in Delphi — classes are heap-allocated and need Free(), records are value types and do not.
-
If you are using 'as' for typecasting, ensure both the source and target are class types that share an inheritance chain.
The 'as' operator only works with class and interface types. For other types, use a hard typecast: Integer(MyValue).
-
If you are inheriting from a type, verify the base type is a class. All Delphi classes ultimately inherit from TObject.
If you need both record-like value semantics and class-like behavior, consider using a class with private fields and properties.
-
For exception types, the class must inherit from Exception (which itself inherits from TObject via ESomething chains).
Always declare custom exceptions as: type EMyError = class(Exception);
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
What is the difference between a class and a record in Delphi?
A class is a reference type — a variable holds a pointer to the object on the heap, and you must call Create and Free. A record is a value type — the data is stored directly in the variable, no heap allocation needed. Classes support inheritance, virtual methods, and interfaces. Records (in modern Delphi) support methods and operator overloading but not inheritance.
Can I add methods to a record in Delphi?
Yes — modern Delphi (Delphi 2006 and later) allows records to have methods, properties, and operator overloads. However, records cannot inherit from other records or implement interfaces (with the exception of advanced records in newer Delphi versions). If you need inheritance, you must use a class.
When should I use a record versus a class?
Use a record for small, data-only structures where value semantics make sense — like a Point(X, Y) or a Color(R, G, B). Use a class for objects that have behavior, need lifetime management, or participate in inheritance hierarchies. Records are more efficient for small data because they avoid heap allocation overhead.