For Each and the collection
To loop through a collection
- 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.
- In Solution Explorer, select the Form1.vb node in the PlayerTest project, and then from the View menu, choose Designer.
- Select the ComboBox control. Then, in the Properties window, select the Items property and click the … button.
- In the String Collection Editor, delete the four existing entries, and then click OK.
- Double-click the form to open the Code Editor.
- In the Code Editor, add the following to the declarations section (below the declaration for team).
An VB example
Dim player As Persons.Players For Each player In team ComboBox1.Items.Add(player.Position) Next
- 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.