Search code examples
delphidelphi-10-seattle

H2161 Duplicate resource [Can a VCL project have 2 forms with the same class name but different namespaces?]


I tried creating 2 forms with the same class name in 2 different namespaces

FirstNameSpace.ExampleFormName.TExampleFormName
SecondNameSpace.ExampleFormName.TExampleFormName

although this compiles, I get the following hint

[dcc32 Hint] H2161 Warning: Duplicate resource: Type 10 (RCDATA), ID TEXAMPLEFORMNAME; File FirstNameSpace.ExampleFormName.TExampleFormName.DFM resource kept; file SecondNameSpace.ExampleFormName.TExampleFormName.dfm resource discarded.

and the program crashes when referencing TExampleFormName.

It looks like there isn't enough information for the linker to work correctly.

Is there any way to make this work?


Solution

  • No. As is indicated in the nature and content of the error, the class-name reference in the associated form files (.dfm) is not namespace qualified and neither are the corresponding resource ID's.

    Form class-names must be unique within/across an application.

    Similarly, class names of components referenced in a DFM (including controls placed on the form) must also be unique since these also are not namespace qualified.

    To promote/ensure unique component/control classnames, a system of prefixes has been adopted by vendors and component developers. That is, Every class produced by a vendor or in a suite of components etc will share a common prefix in addition to their usual name.

    For example, if a company called ACME were to provide a library of enhanced, standard UI controls, they might name them:

    TAcmeEdit
    TAcmeButton
    TAcmeListbox
    etc
    

    In order to distinguish them from the standard (non-prefixed) VCL controls or from other vendor controls (using a different prefix).

    The Delphi Prefix Registry is a community run/supported web site maintains a list of these prefixes (of most use to developers of control/component libraries to ensure they pick a prefix that is not already in use).

    I'm not sure how FMX application resources are handled and it may be possible in that case. But just because I don't know that it doesn't work doesn't mean that it does.