Share via


Visual Basic Concepts

What is an Object?

An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like a control or a form. An entire application can also be an object. The following table describes examples of the types of objects you can use in Visual Basic.

Example Description
Command button Controls on a form, such as command buttons and frames, are objects.
Form Each form in a Visual Basic project is a separate object.
Database Databases are objects, and contain other objects, like fields and indexes.
Chart A chart in Microsoft Excel is an object.

Where do Objects Come From?

Each object in Visual Basic is defined by a class. To understand the relationship between an object and its class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie — for instance, size and shape. The class is used to create objects. The objects are the cookies.

Two examples of the relationship between classes and objects in Visual Basic may make this clearer.

  • The controls on the Toolbox in Visual Basic represent classes. The object known as a control doesn't exist until you draw it on a form. When you create a control, you're creating a copy or instance of the control class. That instance of the class is the object you refer to in your application.

  • The form you work with at design time is a class. At run time, Visual Basic creates an instance of the form's class.

The Properties window displays the class and Name property of objects in your Visual Basic application, as shown in Figure 5.8.

Figure 5.8   Object and class names shown in the Properties window

All objects are created as identical copies of their class. Once they exist as individual objects, their properties can be changed. For example, if you draw three command buttons on a form, each command button object is an instance of the CommandButton class. Each object shares a common set of characteristics and capabilities (properties, methods, and events), defined by the class. However, each has its own name, can be separately enabled and disabled, can be placed in a different location on the form, and so on.

For simplicity, most of the material outside of this chapter won't make many references to an object's class. Just remember that the term "list box control," for example, means "an instance of the ListBox class."