Visual Basic Concepts

Programming with Objects

See Also

Objects are central to Visual Basic programming. Forms and controls are objects. Databases are objects. There are objects everywhere you look.

If you've used Visual Basic for a while, or if you've worked through the examples in the first five chapters of this book, then you've already programmed with objects — but there's a lot more to objects than what you've seen so far.

In this chapter, user-defined types will take on personalities of their own, and become classes. You'll see how easy it is to create your own objects from the classes you define, and to use objects to simplify your coding and increase code reuse.

Topics

What You Need to Know About Objects in Visual Basic

Reviews the correct terms to use for creating, working with, and talking about objects. Introduces the Collection object, the With statement, and other techniques for working with objects.

Finding Out About Objects

Explains how to use the Object Browser to discover an object's properties, methods, and events; to find things in projects and libraries; and to add descriptions of your own objects.

Creating Your Own Classes

Creating classes is easy — you may be surprised to learn that you've already done it. A step by step procedure introduces object creation and lifetime issues.

Adding Properties and Methods to a Class

Explains how to use property procedures to create properties that can validate the values that are assigned to them, and shows how to create many different kinds of properties.

Adding Events to a Class

Your objects can raise events that you handle in forms or in other objects.

Creating Data-Aware Classes

It’s easy to bind your object to a database or other source of data. Classes can act as either a source or a consumer of data.

Naming Properties, Methods, and Events

Following a few simple rules can make your objects much easier to use, reuse, and share.

Polymorphism

It's useful to be able to treat both a Tyrannosaur and a Flea as Animals. Polymorphism lets you do this through multiple interfaces — which also provide new ways to let your programs evolve.

Programming with Your Own Objects

Once you've created objects, how do you incorporate them into programs, keep track of them, and clean up after them?

Object Models

How do the objects you create from your classes relate to each other? Object models express the way objects contain other objects; this can be a powerful organizing principle for your programs.

Creating Your Own Collection Classes

Exposes some limitations with the Visual Basic Collection object, and shows how you can create your own collection classes.

ActiveX Designers

Designers are what put the "visual" in Visual Basic. You've used Form designers, but there's a whole new world of possibilities being opened up by ActiveX Designers.


And it doesn't stop here. "Programming with Components" takes the next step, showing how you can use Visual Basic to control objects provided by other applications.

Sample application

ProgWOb.vbp, Dataware.vbp

Some of the code examples in this chapter are taken from the Programming with Objects (ProgWOb.vbp) and Data-aware Classes (Dataware.vbp) samples. You'll find these applications listed in the Samples directory. .