When a control is requested to load persistent data through an exchange object with a version different from the control, it must do something reasonable to set the values of its persistent custom properties. The simplest approach is to ignore the exchange object and initialize each persistent custom property to its default value by modifying the Circle control's DoPropExchange
function. The changes involve using two member functions of the exchange object, GetVersion
and IsLoading
.
The GetVersion
member function returns the exchange object's version. The exchange object's IsLoading
member function returns TRUE if the control is to load values through the exchange object. It returns FALSE if the control is to store values through the exchange object.
The GetVersion
function is used to compare the exchange object's version and the control's version. If the versions are the same, each persistent custom property is serialized as usual. If the versions are different and the IsLoading
function returns TRUE, the Circle control's persistent properties are initialized to their default values. If the versions are different and the IsLoading
function returns FALSE, the Circle control ignores the exchange object. Technically, this situation never occurs, because the Circle control always stores the current version of persistent data.
Modify the DoPropExchange
function to look like this:
void CCircCtrl::DoPropExchange(CPropExchange* pPX)
{
ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
COleControl::DoPropExchange(pPX);
if (pPX->GetVersion() == (DWORD)MAKELONG(_wVerMinor, _wVerMajor))
{
PX_Bool(pPX, _T("CircleShape"), m_circleShape, TRUE);
PX_Short(pPX, _T("CircleOffset"), m_circleOffset, 0);
PX_Long(pPX, _T("FlashColor"), (long &)m_flashColor, RGB(0xFF,
0x00, 0x00));
PX_String(pPX, _T("Note"), m_note, _T(""));
}
else
if (pPX->IsLoading())
{
m_circleShape = TRUE;
m_circleOffset = 0;
m_flashColor = RGB(0xFF, 0x00, 0x00);
m_note = _T("");
}
}
As described earlier, the ExchangeVersion
function sets the exchange object's version. This is the version of persistent data that the control is to load or store through the exchange object. This version is returned by the GetVersion
function.
The control version and stock properties are serialized as usual. The COleControl::DoPropExchange function correctly loads or stores stock property values for different versions because part of what is serialized is information about which stock properties were stored for that version.
If the exchange object's version and the control's version are the same, the Circle control's persistent custom properties are loaded or stored through the exchange object as usual.
If the versions are different, and persistent data is to be loaded, all persistent properties are initialized to their default values instead.
If the versions are different, and persistent data is to be stored, the exchange object is ignored and no persistent data is stored through the exchange object. This case should never happen with the Circle control because the exchange object's version and the control's version may be different only when data is to be loaded through the exchange object. Using FALSE as the third parameter to the ExchangeVersion
function allows a control to specify that persistent data should be stored as the same version that was last loaded.