Our next step is to add property CurRec to IReadPhBook. Property CurRec sets an index within COM object PhBookObj that acts as a current record pointer. Recall from our previous discussions that COM clients access COM objects via a COM interface pointer.
The interface pointer is a pointer to a vtable pointer. The only entries in a vtable are pointers to interface methods, for example COM clients cannot directly access data within a COM object. To allow data access, we use properties. A property is an attribute of a COM object. Within the COM object, a property may directly correlate to a variable or a calculated value. Properties are accessed via "get" and "put" methods. A read-only property does not have a put method.
Lets add get and set methods for property CurRec to IReadPhBook:
Adding read-write property using COM
Here are the Steps required to add a read-write property:
In the project Workspace window with the Class View tab selected, right-click on IReadPhBook.
On the context-sensitive menu, click Add Property... This will bring up the Add Property to Interface dialog.
The first step in adding a property is to set the property type. Open the drop-down menu for Property Type.
Select long as the property type. Normally, the implementation would be visible in the window at the bottom of the dialog.
With the property type set, type CurRec as Property Name. Normally, you would see the implementation as you type in the property name. When you've done this, click OK to finish adding the property.
The Add Property dialog closes. We've expanded the interface IReadPhBook so you can see the property we've added. The project workspace ClassView pane should appear.
Notice that two CurRec methods were added: CurRec(Long newVal) and CurRec(long * pVal). These are the put and get methods respectively. IReadPhBook, within PhBook.idl, now appears as:
Notice the propget and propput attributes assigned to each method. These impose a naming convention on each. For example, the get method is named get_CurRec, the put method is named put_CurRec.
High-level development environments such as Visual Basic use the helpstring attribute to display information about the method. High-level development environments also use the retval attribute, assigned to pVal in get_CurRec. The retval attribute is translated into a return value from the method within the high-level development language. Visual C++ also created stub code for these methods within our COM object implementation class CPhBookObj.
CPhBookObj now has method declarations for get_CurRec and
put_CurRec in PhBookObj.h.
Adding a read-write property
class CPhBookObj :
public CComCoClass < CPhBookObj,&CLSID_PhBookObj>