Learning Resources
 

For Each and the collection


To loop through a collection

  1. Open the Persons project from the previous lesson. If you did not finish it, go back to the previous lesson, Keeping Track Of Things: Using Collections to Manage Multiple Objects, and complete the procedures.

  2. In Solution Explorer, select the Form1.vb node in the PlayerTest project, and then from the View menu, choose Designer.

  3. Select the ComboBox control. Then, in the Properties window, select the Items property and click the button.

  4. In the String Collection Editor, delete the four existing entries, and then click OK.

  5. Double-click the form to open the Code Editor.

  6. In the Code Editor, add the following to the declarations section (below the declaration for team).

    Dim player As Persons.Players
    For Each player In team
        ComboBox1.Items.Add(player.Position)
    Next
    
    
  7. Press F5 to run the program. Select a position from the drop-down list. The player for that position is displayed in a message box.

--MSDN