Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1
297

Chapter 12


Working with Collections


After completing this chapter, you will be able to:
n Manipulate the Controls collection on a form.
n Use a For Each... Next loop to cycle through objects in a collection.
n Create your own collections for managing Web site URLs and other information.
n Use VBA collections within Microsoft Office.
In this chapter, you’ll learn how to use groups of objects called collections in a Microsoft
Visual Basic program. You’ll learn how to manage information with collections and process
collection objects by using For Each... Next loops. When you combine collection- processing
skills with what you learned about arrays in Chapter 11, “Using Arrays to Manage Numeric
and String Data,” you’ll have much of what you need to know about managing data
effectively in a program, and you’ll have taken your first steps in manipulating the object
collections exposed by Microsoft Visual Studio 2010 and popular Windows applications.

Working with Object Collections


In this section, you’ll learn about collections, a powerful mechanism for controlling objects
and other data in a Visual Basic program. The Microsoft .NET Framework maintains several
standard object collections that you can use when you write your programs. You can use
Visual Studio to browse your system for collections and other application objects.

You already know that objects on a form are stored together in the same file. But did you
also know that Visual Basic considers the objects to be members of the same group? In Visual
Studio terminology, the entire set of objects on a form is called the Controls collection. The
Controls collection is created automatically when you open a new form, and when you add
objects to the form, they become part of that collection.

Each collection in a program has its own name so that you can reference it as a distinct unit
in the program code. For example, you use the Controls name to reference the collection of
objects on a form. This grouping method is similar to the way arrays group a list of elements
together under one name, and like Visual Basic arrays, the Controls collection is zero-based.

If you have more than one form in a project, you can create public variables associated with
the form names and use those variables to differentiate one Controls collection from another.
(You’ll learn more about using public variables to store form data in Chapter 14, “Managing
Windows Forms and Controls at Run Time .”) You can even add controls programmatically to
the Controls collection in a form.

Table of Contents


Working with Collections.................................. 297


Working with Object Collections...................................... 297
Referencing Objects in a Collection.............................. 298
Writing For Each... Next Loops................................. 298
Experimenting with Objects in the Controls Collection............. 299
Using the Name Property in a For Each... Next Loop.............. 302
Creating Your Own Collections....................................... 304
Declaring New Collections...................................... 304
One Step Further: VBA Collections.................................... 309
Entering the Word Macro....................................... 310
Chapter 12 Quick Reference.......................................... 311
Free download pdf