Visual Basic Concepts

Creating a Data Consumer

In this section, we’ll walk step-by-step through the process of creating a data-aware class that acts as a data consumer. The previous section, "Creating a Data Source," demonstrates how to create a data source to which a data consumer can be bound. This example shows how to create a data consumer class and bind it to the data source created in the previous section.

The code examples in this section are taken from the Data-aware Classes (Dataware.vbp) sample. You'll find this application in the directory.

Binding a Data Consumer to a Data Source Object

This example demonstrates how to create a data consumer class and bind it to a data source class. The example uses the MySource class created in the "Creating a Data Source" topic.

  1. Open the Dataware.vbp project. (Select Open Project from the File menu.)

    Note   If you haven’t previously completed the "Creating a Data Source" example this project won’t exist. You can also find a completed version of the Dataware.vbp project in the directory.

  2. Insert a new class module by selecting Add Class Module from the Project menu.

  3. In the Properties window, set the properties of the new class as follows:

Property Setting
Name MyConsumer
DataBindingBehavior vbSimpleBound
  1. Add the following to the Declarations section of the class module:

    Option Explicit
    Private mDirectory As String
    
  2. Add a pair of Property Get / Property Let procedures for a public DirName property:

    Public Property Get DirName() As String
       DirName = mDirectory
    End Property
    
    Public Property Let DirName(mNewDir As String)
       mDirectory = mNewDir
       ' Display the new value in the Immediate window.
       Debug.Print mDirectory
    End Property
    

    Since MySource is a nonvisual class, we need to use a Debug.Print statement in the Property Let procedure to prove that it’s retrieving new values from the data source.

  3. Select Form1 and add the following code to the Declarations section:

    Option Explicit
    Private objSource As MySource
    Private objBindingCollection As BindingCollection
    Private objConsumer As MyConsumer
    

    The new line of code adds a reference to our consumer class.

  4. Add the following code to the Form Load event procedure:

    Private Sub Form_Load()
       Set objSource = New MySource
       Set objBindingCollection = New BindingCollection
       Set objConsumer = New MyConsumer
    
       ' Assign the source class to the Binding
       ' Collection’s DataSource property.
       Set objBindingCollection.DataSource = objSource
       ' Add a binding.
       objBindingCollection.Add txtConsumer, "Text", "Directory"
       objBindingCollection.Add objConsumer, "DirName", "Directory"
    

    The new code creates an instance of the consumer class and adds it to the Binding Collection, binding the DirName property of the consumer to the Directory field of the data source.

  5. Press F5 to run the project. Make sure that the Immediate window is visible.

    As you click the Cycle button, the directory names provided by MySource will appear in both the TextBox and the Immediate window, proving that MyConsumer is bound to MySource.