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:
We add read-only properties the same way we added CurRec. Right-click IReadPhBook in the ClassView pane of the Workspace window.
Select Add Property ... and click OK. This will bring up the Add Property to Interface dialog.
In the Add Property to Interface dialog, open the Property Type drop-down menu.
Choose long as the property type.
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.
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.
Select Add Property ... and click OK. This will bring up the Add Property to Interface dialog.
In the Add Property to Interface dialog, open the Property Type drop-down menu.
Choose long as the property type.
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.
The property NumRecs is added to IReadPhBook. This is the end of the simulation.
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:
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