ClassWizard: Foreign Objects

HomeOverviewHow Do ITutorial

ClassWizard extends dialog data exchange (DDX) by allowing you to map controls on a form or dialog box indirectly to a "foreign object" — a CRecordset or CDaoRecordset object. This article explains:

Foreign Objects

The best way to understand foreign objects is to consider DDX for a dialog box or form view and then see how it is extended. This discussion considers a form view, based on class CFormView.

DDX maps the controls in a form view to data members of the CFormView-derived class associated with the dialog-template resource. Controls in a form view correspond one-to-one with form view class data members. Two entities are connected: the form view on the screen and the form view class. (Dialog boxes work this way too.)

DDX for foreign objects connects not two entities but three. The following figure shows this connection, with a record view on screen at one end, a CRecordView class in the middle, and a third object — the foreign object — at the other end: a CRecordset object. (These could be a CDaoRecordView and a CDaoRecordset.)

DDX for Foreign Objects

The record view class in the preceding figure contains a member variable, a pointer whose type matches the class of the foreign object. The DDX mapping is from three record view controls to members of the foreign object through the pointer, as illustrated by the following DoDataExchange function:

void CSectionForm::DoDataExchange(CDataExchange* pDX)
{
  CRecordView::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CSectionForm)
  DDX_FieldText(pDX, IDC_COURSE,
    m_pSet->m_strCourseID, m_pSet);
  DDX_FieldText(pDX, IDC_INSTRUCTOR,
    m_pSet->m_strInstructorID, m_pSet);
  DDX_FieldText(pDX, IDC_ROOMNO,
    m_pSet->m_strRoomNo, m_pSet);
  //}}AFX_DATA_MAP
} 

Notice the indirect reference to recordset fields, through the m_pSet pointer to a CSections recordset object:

m_pSet->m_strCourseID

Also notice that m_pSet is repeated as the fourth argument in each DDX_FieldText call.

ClassWizard and Foreign Objects

You set the foreign class and object on ClassWizard's Class Info tab. The Foreign Class box names the class of the foreign object (CRecordset for ODBC or CDaoRecordset for DAO). The Foreign Object box names the pointer variable that points to an object of that class. (You can specify only one foreign object per class.)

If you select a class based on a dialog-template resource on ClassWizard's Member Variables tab, ClassWizard locates the appropriate pointer variable in the class and puts its type into the Foreign Class box. It also puts the variable name into the Foreign Object box. You can change these values if you wish.

If you create a CRecordView or CDaoRecordView class with AppWizard and then use ClassWizard to specify the DDX connections, ClassWizard automatically notes the presence in the view class of a pointer to a CRecordset-derived or CDaoRecordset-derived object. ClassWizard sets this as the value in the Foreign Class box. It puts the name of the pointer variable into the Foreign Object box.

See Also   ClassWizard: Mapping Form Controls to Recordset Fields