Share via


COLLECT Sample: Illustrates MFC Collection Classes

The COLLECT sample illustrates a wide variety of collection classes offered by the Microsoft Foundation Class Library (MFC).

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

To build and run the COLLECT sample

  1. Open the solution collect.sln.

  2. On the Build menu, click Build.

  3. On the Debug menu, click Start Without Debugging.

The Example menu lists nine collection class illustrations organized according to the following table. Each example presents a form view that exercises many common collection operations:

  • Adding a new element.

  • Inserting a new element in the middle of a list.

  • Searching an element (by clicking its entry in the dialog box's list box).

  • Searching an entry in the map by entering the Key and clicking Find.

  • Updating the value of an element.

  • Removing an element.

  • Removing all the elements of the collection.

You can save changes to all the examples by clicking Save on the File menu and read them back by clicking Open on the File menu.

MFC Collection Classes

The MFC collection classes can be classified according to shape and design. MFC offers classes for three types of collection shapes:

  • Lists

  • Arrays

  • Maps

MFC also offers three types of designs:

  • No use of C++ templates.

  • Use of a C++ template–based collection of simple elements.

  • Use of a C++ template–based collection of type-safe pointers.

The following table identifies all the MFC collection classes sorted according to shape and design. The COLLECT sample illustrates nine of these collections, one from each cell in the table. Each represents a distinct combination of shape and design. Also, because usage of the CTypedPtrArray and CTypedPtrList template-based collections differs according to whether they are used to hold pointers to CObjects or to non-CObjects, the COLLECT sample illustrates both cases. The nine collections illustrated by COLLECT are indicated with an asterisk.

Collection shapes

Non-template based

Template-based simple elements

Template-based typeSafe pointers

List

CStringList*

CList*

CTypedPtrList

 

CObList

 

(of pointers)*

 

CPtrList

 

CTypedPtrList

 

 

 

(of CObjects)

Array

CByteArray

CArray*

CTypedPtrArray

 

CUIntArray

 

(of pointers)

 

CWordArray*

 

 

 

CDWordArray

 

CTypedPtrArray

 

CStringArray

 

(of CObjects)*

 

CPtrArray

 

 

 

CObArray

 

 

Map

CMapWordToPtr

CMap*

CTypedPtrMap*

 

CMapWordToOb

 

 

 

CMapStringToString*

 

 

 

CMapStringToPtr

 

 

 

CMapStringToOb

 

 

 

CMapPtrToWord

 

 

 

CMapPtrToPtr

 

 

Additional COLLECT Features

In addition to illustrating the MFC collection classes, COLLECT also illustrates:

  • Coordinating the addition and removal of entries in a list box with the addition and removal of entries in a collection.

  • Switching between different views of a document, with only one shown at a time. That is, although COLLECT has many different views, it has a single document interface (SDI) instead of a multiple document interface (MDI).

Keywords

This sample demonstrates the following keywords:

AfxMessageBox; CCmdUI::SetCheck; CControlBar::EnableDocking; CDialog::DoModal; CDocument::DeleteContents; CDocument::OnNewDocument; CFrameWnd::DockControlBar; CFrameWnd::EnableDocking; CFrameWnd::GetActiveDocument; CFrameWnd::GetActiveView; CFrameWnd::RecalcLayout; CFrameWnd::SetActiveView; CListBox::AddString; CListBox::DeleteString; CListBox::FindString; CListBox::GetCurSel; CListBox::GetItemData; CListBox::GetItemDataPtr; CListBox::GetText; CListBox::InsertString; CListBox::ResetContent; CListBox::SetItemData; CListBox::SetItemDataPtr; CObject::AssertValid; CObject::Dump; CObject::Serialize; CStatusBar::Create; CStatusBar::SetIndicators; CString::Empty; CString::Format; CString::LoadString; CToolBar::Create; CToolBar::LoadBitmap; CToolBar::SetButtons; CView::DoPreparePrinting; CView::GetDocument; CView::OnBeginPrinting; CView::OnDraw; CView::OnEndPrinting; CView::OnInitialUpdate; CView::OnPreparePrinting; CView::OnUpdate; CWinApp::AddDocTemplate; CWinApp::InitInstance; CWinApp::LoadStdProfileSettings; CWinApp::OnFileNew; CWnd::DestroyWindow; CWnd::DoDataExchange; CWnd::OnCreate; CWnd::ShowWindow; CWnd::UpdateData; LoadString; SetWindowLong

Note

Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

Other Resources

MFC Samples