4

Can I use a DataModule form in a Console Delphi application?

I tried including an existing unit/form to a console Delphi application in the uses clause, but DataModule1 is resolved to nil (not initialized).

1
  • TDataModule is not a Form.
    – kobik
    Feb 19, 2013 at 11:58

1 Answer 1

8

Just figured that by looking at a Form application that I simply need to do the initialization by hand:

Application.CreateForm(TDataModule1, DataModule1);

UPDATE

As suggested in the comments, I ended up implementing:

DataModule1 := TDataModule1.Create(nil)
6
  • Or use the Project Options dialog to set the data module as auto-create "form" which has the same effect. Feb 18, 2013 at 22:25
  • 7
    Personally I don't like relying on the .dpr file to do this. Add that code to the initialization section of the data module unit. That makes the unit self-contained. And I see no reason to use Application.CreateForm either. Just call the constuctor, DataModule1 := TDataModule1.Create(Application). Feb 18, 2013 at 22:25
  • I don't get it, using Application in a Console application? Doesn't that practically defeat the purpose of a console application? Feb 19, 2013 at 6:02
  • 2
    @David,@Jerry We don't need to involve Application object for this. simple DataModule1 := TDataModule1.Create(nil) and when you're done with it DataModule1.Free.
    – kobik
    Feb 19, 2013 at 11:55
  • 1
    Exactly my point, why rely on Application at all in a console app? Feb 19, 2013 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.