Abstraction

Abstraction is the process by which you can think about specific properties or behaviors without thinking about a particular object that has those properties or behaviors. Abstraction is merely the ability of a language to create “black box” code, to take a concept and create an abstract representation of that concept within a program.

A Customer object, for example, is an abstract representation of a real-world customer. A DataSet object is an abstract representation of a set of data.

Abstraction enables you to recognize how things are similar and to ignore differences, to think in general terms and not in specifics. A TextBox control is an abstraction because you can place it on a form and then tailor it to your needs by setting properties. Visual Basic enables you to define abstractions using classes.

Any language that enables a developer to create a class from which objects can be instantiated meets this criterion, and Visual Basic is no exception. You can easily create a class to represent a customer, essentially providing an abstraction. You can then create instances of that class, whereby each object can have its own attributes, representing a specific customer.

In Visual Basic, you implement abstraction by creating a class using the Class keyword. To see this in action, right-click on your solution and select Add New Project. From the Add New Project dialogue select a Visual Basic Windows Forms Application project and name it “VB2012_ObjectDataSource.” Once the project is open, add a new class to the project using the Project ⇒ Add Class menu option. Name the new class Customer.vb, and add the following code to make this class represent a real-world customer in an abstract sense (code file: customer.vb):

Public Class Customer
   Public Property ID As Guid
   Public Property Name As String
   Public Property Phone As String
End Class

You know that a real customer is a lot more complex than an ID, a name, and a phone number; but at the same time, you know that in an abstract sense, your customers really do have names and phone numbers, and that you assign them unique ID numbers to keep track of them.

You can then use this abstract representation of a customer from within your code by using data binding to link the object to a form. First, build the project by selecting Build ⇒ VB2012_ObjectDataSource. Then click the Data ⇒ Show Data Sources menu option to open the Data Sources window. Select the Add New Data Source link in the window to bring up the Data Source Configuration Wizard. Within the wizard, choose to add a new Object data source, click Next, and then select your Customer class, as shown in Figure 4.10.

Figure 4.10 Data Source Configuration Wizard

4.10

Finish the wizard. The Customer class will be displayed as an available data source, as shown in Figure 4.11, when you are working in Design view for Form1.

Figure 4.11 Data Sources Window in Visual Studio

4.11

Click on Customer in the Data Sources window. Customer should change its display to a combo box. Open the combo box and change the selection from DataGridView to Details. This way, you get a Details view of the object on your form. Open the designer for Form1 and drag the Customer class from the Data Sources window onto the form. The result in design view should look something like the dialog shown in Figure 4.12.

Figure 4.12 Viewing the generated form in the designer

4.12

All you need to do now is add code to create an instance of the Customer class to act as a data source for the form. Double-click on the form to bring up its code window and add the following:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) _
                           Handles MyBase.Load
        Me.CustomerBindingSource.DataSource = New Customer()
    End Sub
End Class

You are using the ability of Windows Forms to data-bind to a property on an object. You'll learn more about data binding later. For now, it is enough to know that the controls on the form are automatically tied to the properties on your object.

Now you have a simple user interface (UI) that both displays and updates the data in your Customer object, with that object providing the UI developer with an abstract representation of the customer. When you run the application, you will see a display like the one shown in Figure 4.13.

Figure 4.13 Running the sample code

4.13
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset