Visual Basic Concepts

Creating Data-Aware Classes

If you’ve read the preceding material on creating classes, you know by now that a class is an object that encapsulates data and code, and that the properties of a class are the data that describe an object. You also know that you can use property procedures or public properties to expose the data represented by those properties.

So far, so good — all of the examples thus far have dealt with transient data, that is, data that is created and consumed at run time. For many programs, this may be all that you need, but what if you need to store data between sessions, or utilize data that already exists outside of your program? In order to work with external sources of data, you need to make your class data-aware.

Data-aware classes can be divided into two categories — data consumers and data sources. Class modules have two design-time properties, DataBindingBehavior and DataSourceBehavior, that determine how a class will interact with external data. The BindingCollection object is used to bind data-aware classes to controls or to each other.

Data Sources

A data source is a class that provides data from an external source to be consumed by other objects. A Data control is in reality an instance of a class that is a data source, but classes that have been set up to act as data sources can be much more powerful than a Data control. Unlike the Data control, a data-aware class doesn’t have to have a visual representation, and it isn’t limited to a particular data interface such as Data Access Objects (DAO) or Remote Data Objects (RDO). In fact, a data-aware class can act as a data source for any type of data, including traditional ODBC sources, ActiveX Data Objects (ADO), or any OLE DB provider.

The DataSourceBehavior property determines whether or not a class can act as a data source. By setting the DataSourceBehavior to 1 (vbDataSource), your class can act as a source of data for other objects.

Data Consumers

Simply put, a data consumer is a class that can be bound to an external source of data, much as a TextBox control can be bound to a Data control. In earlier versions of Visual Basic, controls were the only objects that could be bound to a data source. Data-aware classes set up as data consumers allow you to bind any object to any object created from a class that has been set up as a data source.

The DataBindingBehavior property allows a class to bind to external data. By setting this property to 1 (vbSimpleBound), an object created from your class will be bound to a single data field in an external data source. By setting the DataBindingBehavior to 2 (vbComplexBound), your class will be bound to a row of data in an external data source. Think of it this way — if your objects were controls, a TextBox control would be simple bound, whereas a grid control would be complex bound.

The BindingCollection Object

Just as you would bind a control to a database through a Data control, data-aware classes need a central object to bind them together. That object is the BindingCollection object. Just as it sounds, the BindingCollection is a collection of bindings between a data source and one or more data consumers.

In order to use the BindingCollection object you must first add a reference to the Microsoft Data Binding Collection by selecting it in the References dialog, available from the Project menu. As with any object, you’ll need to create an instance of the BindingCollection object at run time.

The DataSource property of the BindingCollection object is used to specify the object that will provide the data. This object must be a class or UserControl with its DataSourceBehavior property set to vbDataSource.

Once the BindingCollection has been instantiated and its DataSource set, you can use the Add method to define the binding relationships. The Add method takes three required arguments: the name of the consumer object, the property of that object to be bound to the source, and the field from the source that will be bound to the property. You can add multiple bindings to the BindingCollection by repeating the Add method; you can use the Remove method to delete a binding.

*For More Information* For step-by step examples of creating data-aware classes, see "Creating a Data Source" and "Creating a Data Consumer."