OOPortal OOPortal

ATL Development   «Prev  Next»
Lesson 11 Adding a read-write property
Objective Add the CurRec property to IReadPhBook.

Adding read-write Property

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: Steps To Add Read Write Property
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:
 helpstring("IReadPhBook Interface"),
interface IReadPhBook : IUnknown
      [propget, helpstring("property CurRec")]
      HRESULT CurRec([out, retval] long *pVal);
      [propput, helpstring("property CurRec")]
      HRESULT CurRec([in] long newVal);

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.
Stub object created
Stub object created

CPhBookObj now has method declarations for get_CurRec and put_CurRec in PhBookObj.h.
Implementation file PhBookObj.cpp has stub methods for get_CurRec and put_CurRec.

Add Read Write Property - Exercise

Click the Exercise link below to apply what you have learned about adding read-write properties to a COM object.
Add Read Write Property - Exercise