Take the 2-minute tour ×

I've a document class which has an object which is a connection to a database. The document class has a dialog box which is spawned from a menu, which contains a combo box. I'm currently using OnInitDialog to set the items in the combo box but want to use the database connection.

What is the best way of supplying the database connection object to the Dialog box object? I don't think I can pass it in as an argument, and if I supply it after construction, I might have missed the call to the OnInitDialog. Is there a way of finding the parent object and calling a getMethod to obtain the database object or am I on the wrong track altogether and should be attempting something completely different?

Thanks for your help, James

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Normally you would subclass the dialog to create a custom constructor which includes whatever objects you want to work on e.g. your database object.

share|improve this answer
Why need to subclass? You can just modify the constructor –  zadane May 30 '12 at 14:39
Thank you very much, I went with the sub-class option and it works really well so far. Much appreciated. –  James May 31 '12 at 12:52
add comment

You should pass the database connection object in the dialog's constructor. Modify the dialog constructor or add another constructor accordingly. You may declare other plain constructors of the dialog as private so passing on the db connection becomes a requirement for the dialog.

share|improve this answer
add comment

I am not sure about architecture you are referring i.e. Dialog box inside the document class but I think you can add a member variable for database connection inside dialog box class and then set the db connection varaible in your dialog box constructor or dlg->dbConnection = dbConn; before calling DoModal or ShowWindow

share|improve this answer
add comment

Your Answer


