E2039
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
E2039 means you declared the same identifier name twice within the same scope, which Delphi does not allow. This can happen with variables, constants, type names, or unit names that clash with each other. Fix it by renaming one of the conflicting identifiers or removing the duplicate declaration.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- Declaring a local variable with the same name as a parameter in the same function
- Declaring a type or constant with the same name as an existing identifier in the unit
- Two units in the uses clause that both declare an identifier with the same name
- Accidentally copying a var declaration block and creating duplicate variable names
- A loop variable or with-block variable that shadows a higher-scope variable declaration
How to Fix It
-
The error message names the duplicate identifier — search the current unit for all declarations of that name.
Use Ctrl+F to search for the identifier name in the current file.
-
Remove or rename the second declaration. Keep the one that is more appropriate for the context.
If both declarations serve different purposes, rename one to be more descriptive, such as adding a prefix or suffix.
-
If the conflict is between a local variable and a parameter, remove the local variable — the parameter is already in scope and can be used directly.
Parameters are treated as local variables in Delphi — you cannot redeclare a parameter name in the var block.
-
If the conflict is between two units in the uses clause, qualify the identifier with the unit name: UnitName.IdentifierName.
Example: SysUtils.StrToInt(S) versus MyHelpers.StrToInt(S) — qualifying disambiguates which one to use.
-
Reorder the uses clause — the last unit listed takes priority for unqualified names. Put the unit you want to take precedence last.
This only works if you cannot or do not want to qualify every usage. Explicit qualification is clearer.
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
Can I have two identifiers with the same name in different scopes?
Yes — Delphi allows the same name in different scopes (a local variable can shadow a global one). However, within the same scope (e.g., both declared in the same var block or same parameter list) duplicates are not allowed. The inner scope declaration hides the outer one, which can itself cause subtle bugs.
What happens when two units declare a type with the same name?
If both units are in the uses clause and you use the name without qualification, Delphi uses whichever unit is listed last. This can cause unexpected behavior if the two types are different despite having the same name. Always qualify the identifier with the unit name when there is any ambiguity.
Is E2039 different from a warning about shadowing?
Yes — E2039 is a hard compiler error for a true duplicate within the same scope. Shadowing (where a local name hides an outer scope name) is typically a warning or hint, not an error. Both situations are worth resolving because shadowing can cause logic bugs even when the code compiles.