Partager via


CObList::SetAt

Sets the element at a given position.

void SetAt(
   POSITION pos,
   CObject* newElement 
);

Parameters

  • pos
    The POSITION of the element to be set.

  • newElement
    The CObject pointer to be written to the list.

Remarks

A variable of type POSITION is a key for the list. It is not the same as an index, and you cannot operate on a POSITION value yourself. SetAt writes the CObject pointer to the specified position in the list.

You must ensure that your POSITION value represents a valid position in the list. If it is invalid, then the Debug version of the Microsoft Foundation Class Library asserts.

The following table shows other member functions that are similar to CObList::SetAt.

Class

Member Function

CPtrList

void SetAt( POSITION pos, const CString& newElement );

CStringList

void SetAt( POSITION pos, LPCTSTR newElement );

Example

See CObList::CObList for a listing of the CAge class.

CObList list;
CObject* pa;
POSITION pos;

list.AddHead(new CAge(21));
list.AddHead(new CAge(40)); // List now contains (40, 21).
if ((pos = list.GetTailPosition()) != NULL)
{
    pa = list.GetAt(pos); // Save the old pointer for 
                          //deletion.
    list.SetAt(pos, new CAge(65));  // Replace the tail 
                                      //element.
    delete pa;  // Deletion avoids memory leak.
}
#ifdef _DEBUG
   afxDump.SetDepth(1);
   afxDump << _T("SetAt example: ") << &list << _T("\n");
#endif      

The results from this program are as follows:

SetAt example: A CObList with 2 elements

a CAge at $4D98 40

a CAge at $4DB8 65

Requirements

Header: afxcoll.h

See Also

Reference

CObList Class

Hierarchy Chart

CObList::Find

CObList::GetAt

CObList::GetNext

CObList::GetPrev

Other Resources

CObList Members