Compartilhar via


CObArray::SetAt

Sets the array element at the specified index.

void SetAt( 
   INT_PTR nIndex, 
   CObject* newElement  
);

Parameters

  • nIndex
    An integer index that is greater than or equal to 0 and less than or equal to the value returned by GetUpperBound.

  • newElement
    The object pointer to be inserted in this array. A NULL value is allowed.

Remarks

SetAt will not cause the array to grow. Use SetAtGrow if you want the array to grow automatically.

You must ensure that your index value represents a valid position in the array. If it is out of bounds, then the Debug version of the library asserts.

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

Class

Member Function

CByteArray

void SetAt( INT_PTRnIndex, BYTE newElement );

CDWordArray

void SetAt( INT_PTRnIndex, DWORD newElement );

CPtrArray

void SetAt( INT_PTRnIndex, void* newElement );

CStringArray

void SetAt( INT_PTRnIndex, LPCTSTR newElement );

CUIntArray

void SetAt( INT_PTRnIndex, UINT newElement );

CWordArray

void SetAt( INT_PTRnIndex, WORD newElement );

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

CObArray arr;
CObject* pa;

arr.Add(new CAge(21)); // Element 0
arr.Add(new CAge(40)); // Element 1 
if ((pa = arr.GetAt(0)) != NULL)
{
   arr.SetAt(0, new CAge(30));  // Replace element 0. 
   delete pa; // Delete the original element at 0.
}
#ifdef _DEBUG
   afxDump.SetDepth(1);
   afxDump << _T("SetAt example: ") << &arr << _T("\n");
#endif      

The results from this program are as follows:

SetAt example: A CObArray with 2 elements

[0] = a CAge at $47E0 30

[1] = a CAge at $47A0 40

Requirements

Header: afxcoll.h

See Also

Reference

CObArray Class

Hierarchy Chart

CObArray::GetAt

CObArray::SetAtGrow

CObArray::ElementAt

CObArray::operator [ ]