Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

304 Part II Programming Fundamentals



  1. Click the third button object six or seven times.


As you click the button, the top two button objects move across the screen. The third
button stays in the same place, however, as shown here:


  1. Click the Close button on the form to stop the program.


Giving one object in a collection special treatment can be very useful. In this case, using the
Name property in the For Each... Next loop improved the readability of the program code,
suggesting numerous potential uses for a game or graphics program. As you use other types
of collections in Visual Basic, be sure to keep the Name property in mind.

Creating Your Own Collections


With Visual Basic, you can also create your own collections to track data in a program
and manipulate it systematically. Although collections are often created to hold objects,
such as user interface controls, you can also use collections to store numeric or string
values while a program is running. In this way, collections nicely complement the
capabilities of arrays, which you learned about in Chapter 11.

Declaring New Collections


New collections are declared as variables in a program, and the location in which you declare
them determines their scope, or the extent to which their assigned values persist. Because
collections are so useful, I usually declare them at the top of a form or in a module.

New collection declarations require the syntax:

Dim CollectionName As New Collection()

where CollectionName is the name of your collection. If you place the collection declaration
in a module, you use the Public keyword instead of the Dim keyword. After you create
Free download pdf