ATL Development   «Prev  Next»
Lesson 12 Adding read-only properties
Objective Add MaxRecs and NumRecs to IReadPhBook.

Adding read-only Properties

We can now add read-only properties MaxRecs and NumRecs to interface IReadPhBook. MaxRecs holds the maximum number of phone records, and NumRecs holds the number of phone records currently stored in PhBookObj.
The following text will step you through the process of adding both MaxRecs and NumRecs.

Add Max Records Num Records

Here are the steps through the process of adding both MaxRecs and NumRecs:
  1. We add read-only properties the same way we added CurRec. Right-click IReadPhBook in the ClassView pane of the Workspace window.
  2. Select Add Property ... and click OK. This will bring up the Add Property to Interface dialog.
  3. In the Add Property to Interface dialog, open the Property Type drop-down menu.
  4. Choose long as the property type.
  5. Once you've chosen long as the property type, type MaxRecs as the property name. Because this is to be a read-only property, uncheck the Put function under Function Type. When you're done, click OK to finish.
  6. The property MaxRecs has been added to IReadPhBook. Next we want to add NumRecs as a read-only property. Right-click IReadPhBook in the ClassView pane of the Workspace window.
  7. Select Add Property ... and click OK. This will bring up the Add Property to Interface dialog.
  8. In the Add Property to Interface dialog, open the Property Type drop-down menu.
  9. Choose long as the property type.
  10. Once you chose long as the property type, type NumRecs as the property name. Because this is a read-only property, uncheck Put function. When you are done, click OK.
  11. The property NumRecs is added to IReadPhBook. This is the end of the simulation.

In PhBook.idl, IReadPhBook is now:
[
 object,
 uuid(EF7C3D7D-653E-11D2-85DB-08001700C57F),
 helpstring("IReadPhBook Interface"),
 pointer_default(unique)
]
interface IReadPhBook : IUnknown
{
     [propget, helpstring("property CurRec")
  ] HRESULT CurRec([out, retval] long *pVal);
  
     [propput, helpstring("property CurRec")
  ] HRESULT CurRec([in] long newVal);
  

     [propget, helpstring("property MaxRecs")
  ] HRESULT MaxRecs([out, retval] long *pVal);
  
     [propget, helpstring("property NumRecs")
  ] HRESULT NumRecs([out, retval] long *pVal);


};


Both MaxRecs and NumRecs are marked with the propget attributes. Visual C++ will generate methods get_MaxRecs and get_NumRecs.
The ClassView pane of the Workspace windows now shows now contains:
MaxRecs and NumRecs added.
MaxRecs and NumRecs added

Adding the properties NumRecs and MaxRecs adds method declarations to CPhBookObj.h in PhBookObj.h.

CPhBookObj

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

DECLARE_REGISTRY_RESOURCEID(IDR_PhBookObj)

public:

  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);
};
It also adds stub methods to the implementation file PhBookObj.cpp.

Add Stub PhBookObj.cpp

STDMETHODIMP CPhBookObj::get_CurRec(long *pVal)
{
  // TODO: Add your implementation code here

  return S_OK;
}

STDMETHODIMP CPhBookObj::put_CurRec(long newVal)
{
  // TODO: Add your implementation code here

  return S_OK;
}

STDMETHODIMP CPhBookObj::get_MaxRecs(long *pVal)
{
  // TODO: Add your implementation code here

  return S_OK;
}

STDMETHODIMP CPhBookObj::get_NumRecs(long *pVal)
{
  // TODO: Add your implementation code here

  return S_OK;
}

Add Read Only Properties - Exercise

Click the Exercise link below to apply what you have learned about adding read-only properties to a COM object.
Add Read Only Properties - Exercise