ATL Development   «Prev  Next»
Lesson 14 Adding application data
Objective Add phone record management data to CPhBookObj.

Adding COM Application Data

In this lesson, we will add internal member variables and methods to CPhBookObj to support storing and managing PhRec structures. The first step is to add a constant for the maximum number of phone records. Open PhBookObj.h and add #define MAX_RECS 32 after #include resource.h.
After we have added the constant for the maximum number of phone records, we have to:
  1. Add an array of PhRecs to CPhBookObj
  2. Add a member variable, m_numrecs, to hold the number of records currently stored in m_PhRecs
  3. Add member variable m_currec to hold the value of the CurRec property; a value of -1 indicates that CurRec is not set

Adding Application Data

Here are the steps to add internal member variables and methods to CPhBookObj to support storing and managing PhRec structures:
  1. To add an array of PhRecs to CPhBookObj, right-click CPhBookObj in the ClassView pane of the Workspace window.
  2. Select Add Member Variable... to bring up the Add Member Variable dialog.
  3. In the Add Member Variable dialog, type PhRec for Variable Type and m_PhRecs[MAX_RECS] for Variable Name, and check Protected. When you are done, click OK.
  4. The variable m_PhRecs is added to the Class View under CPhBookObj. The next step is to add the member variable m_numrecs to hold the value of the number of records currently stored in m_PhRecs. Right-click CPhBookObj in the ClassView pane of the Workspace window.
  5. Select Add Member Variable... to open the Add Member Variable dialog.
  6. Type in long for Variable Type, and m_numrecs for Variable Name, and check Protected. When you are done, click OK.
  7. The variable m_numrecs is added to the Class View under CPhBookObj. Now let's add member variable m_currec to hold the value of the CurRec property. A value of -1 indicates that CurRec is not set. Right-click CPhBookObj in the ClassView pane of the Workspace window.
  8. Select Add Member Variable... to bring up the Add Member Variable dialog.
  9. In the Add Member Variable dialog, type long for Variable Type, and m_currec for Variable Name, and check Protected. When you are done, click OK.
  10. The member variable m_currec is added to the Class View under CPhBookObj. This is the end of the simulation.
Finally, once we have added the member variables, we have to initialize m_numrecs and m_currec in the constructor of CPhBookObj in PhBookObj.h.
CPhBookObj() : m_numrecs(0), m_currec(-1)
{} 
Changes to CPhBookObj are reflected in PhBookObj.h.

Class CPhBookObj

class CPhBookObj : 
  public IReadPhBook,
  public IManagePhBook,
  public CComObjectRoot,
  public CComCoClass<CPhBookObj,&CLSID_PhBookObj>
{
public:
  CPhBookObj() : m_numrecs(0), m_currec(-1) {}
BEGIN_COM_MAP(CPhBookObj)
  COM_INTERFACE_ENTRY(IReadPhBook)
  COM_INTERFACE_ENTRY(IManagePhBook)
END_COM_MAP()

DECLARE_REGISTRY_RESOURCEID(IDR_PhBookObj)

public:
  STDMETHOD(DeletePhoneRec)(/*[out]*/ BOOL *pOK);
  STDMETHOD(AddPhoneRec)(/*[in]*/ PhRec *pPhRec,
    /*[out]*/ BOOL *pOK);
   
  STDMETHOD(GetPhoneRec)(/*[in]*/ PhRec *pPhRec,
    /*[out]*/ BOOL *pOK);
    
  STDMETHOD(get_NumRecs)(/*[out, retval]*/
    long *pVal);
  STDMETHOD(get_MaxRecs)(/*[out, retval]*/
    long *pVal);
  STDMETHOD(get_CurRec)(/*[out, retval]*/
    long *pVal);
  STDMETHOD(put_CurRec)(/*[in]*/ long newVal);
protected:

  long m_currec;
  long m_numrecs;
  PhRec m_PhRecs[MAX_RECS];

};

Adding Application Data - Exercise

Click the Exercise link below to apply what you have learned about adding and initializing member variables.
Adding Application Data - Exercise