In my previous article Tweaking DFM Loading I explained ways to overcome the name problem with several instances of a data module. Actually there is still another way to resolve this.
In case you didn’t already, you should read that article first to understand what I am talking about here.
The FOnFindComponentInstance checks Screen.DataModules for a data module with the name in question. If we can avoid adding our data module to that list, the name of the newly created TMainDM instance will remain unchanged.
To skip adding a TDataModule instance to the Screen.DataModules list, we need to override its CreateNew method and change the Dummy parameter to -1.
1 2 3 4 5 |
constructor TMainDM.CreateNew(AOwner: TComponent; Dummy: Integer); begin Dummy := -1; inherited; end; |
As a consequence our global instance of TMainDM will not be found for TMainForm either. Thus we need to create a local instance of TMainDM in TMainForm, too.
1 2 3 4 5 |
constructor TMainForm.Create(AOwner: TComponent); begin TMainDM.Create(Self); inherited; end; |
Now we don’t need that global instance anymore and remove TMainDM from the auto-created list in the project.