void SetAt( LPCTSTR key, CObject* newValue );
throw( CMemoryException );
Parameters
key
Specifies the string that is the key of the new element.
newValue
Specifies the CObject pointer that is the value of the new element.
Remarks
The primary means to insert an element in a map. First, the key is looked up. If the key is found, then the corresponding value is changed; otherwise a new key-value element is created.
Example
See CObList::CObList for a listing of the CAge
class used in all collection examples.
// example for CMapStringToOb::SetAt
CMapStringToOb map;
CAge* pa;
map.SetAt( "Bart", new CAge( 13 ) );
map.SetAt( "Lisa", new CAge( 11 ) ); // Map contains 2
// elements.
#ifdef _DEBUG
afxDump.SetDepth( 1 );
afxDump << "before Lisa's birthday: " << &map << "\n";
#endif
if( map.Lookup( "Lisa", (CObject *&)pa ) )
{ // CAge 12 pointer replaces CAge 11 pointer.
map.SetAt( "Lisa", new CAge( 12 ) );
delete pa; // Must delete CAge 11 to avoid memory leak.
}
#ifdef _DEBUG
afxDump << "after Lisa's birthday: " << &map << "\n";
#endif
The results from this program are as follows:
before Lisa's birthday: A CMapStringToOb with 2 elements
[Lisa] = a CAge at $493C 11
[Bart] = a CAge at $4654 13
after Lisa's birthday: A CMapStringToOb with 2 elements
[Lisa] = a CAge at $49C0 12
[Bart] = a CAge at $4654 13
CMapStringToOb Overview | Class Members | Hierarchy Chart
See Also CMapStringToOb::Lookup, CMapStringToOb::operator []