Using Collections to Manage Multiple Objects
In this lesson, you will learn how to use a collection to manage groups of objects.
In an earlier lesson, you learned about using arrays to manage groups of variables. While you could use arrays to manage groups of objects, Visual Basic also has a special type of object called a collection that can be used to store and retrieve groups of like objects.
Like an array, each item in a Collection object has an index that can be used to identify that item. In addition, each item in a Collection object has a key, a String value that can be used to identify the item. The advantage to using a key is that you do not need to remember the index of an item; instead you can refer to it using a meaningful name.
Creating a Collection
Collections are useful when your program uses multiple instances of the same class. For example, look at the Players class that you created in a previous lesson. It is likely that you need multiple Players objects to represent a baseball team.
The first step in creating a collection is to create an instance of a Collection object, as shown in the following declaration.
Dim baseballTeam As New Collection
Once you create the Collection object, you can use the Add method to add items to it, and the Remove method to delete the items. When adding items, first specify the item to add, and then specify the String value to be used as a key.
baseballTeam.Add(playerObject, "Player's Name")
When removing an item, use the key to specify the item to remove.
baseballTeam.Remove("Player's Name")
In the following procedure, you will add two new Players objects, and then create a team collection and add the Players objects to it, using the Position property as a key.
Try It!
To create a collection of objects
Open the Persons project that you created in the previous lesson. If you did not save it, go back to the previous lesson, Building a Class from an Existing Class: Using Inheritance, and complete the procedures.
In Solution Explorer, in the PlayerTest project, select the Form1.vb node, and then on the View menu, choose Code.
In the Code Editor, add the following to the declarations section (below the declaration for player2).
Dim player3 As New Persons.Players Dim player4 As New Persons.Players Dim team As New Collection
Add the following code to the Form1_Load event procedure.
With player3 .FirstName = "Jeff" .LastName = "Smith" .Number = 52 .Position = "First Base" End With With player4 .FirstName = "Chris" .LastName = "Mayo" .Number = 22 .Position = "Pitcher" End With team.Add(player1, player1.Position) team.Add(player2, player2.Position) team.Add(player3, player3.Position) team.Add(player4, player4.Position)
In Solution Explorer, in the PlayerTest project, select the Form1.vb node. Then on the View menu, choose Designer.
From the Toolbox, drag a ComboBox control onto the form.
In the Properties window, select the Items property and click the … button.
In the String Collection Editor, enter the following, and then click OK.
Catcher
First Base
Pitcher
Shortstop
Double-click the ComboBox control to open the Code Editor, and then enter the following code in the ComboBox1_SelectedIndexChanged event handler.
Dim SelectedPlayer As Persons.Players ' The Trim function deletes spaces at the beginning and ' at the of the string. SelectedPlayer = team(Trim(ComboBox1.SelectedItem)) MsgBox("Playing " & ComboBox1.SelectedItem & " is " & _ SelectedPlayer.FullName & "!")
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.
Next Steps
In this lesson, you learned how to use a Collection object to manage a group of objects. At this point, you can learn more about collections in Closer Look: Using a For Each...Next Loop in a Collection, or you can go on to the next group of lessons and learn about creating your own controls.
Next Lesson: Visible Objects: Creating Your First User Control
See Also
Tasks
Closer Look: Using a For Each...Next Loop in a Collection
Closer Look: Overriding Members
Building a Class from an Existing Class: Using Inheritance