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, bring up Visual Studio and create a new Visual Basic Windows Application project named OOExample. Once the project is open, add a new class to the project using the Project image from book Add Class menu option. Name the new class Customer, and add some code to make this class represent a real-world customer in an abstract sense:

 Public Class Customer    Private mID As Guid = Guid.NewGuid    Private mName As String    Private mPhone As String        Public Property ID() As Guid      Get        Return mID      End Get      Set(ByVal value As Guid)        mID = value      End Set    End Property        Public Property Name() As String      Get        Return mName      End Get      Set(ByVal value As String)        mName = value      End Set    End Property        Public Property Phone() As String      Get        Return mPhone      End Get      Set(ByVal value As String)        mPhone = value      End Set    End Property End Class

You know that a real customer is a lot more complex than an ID, name, and phone number. Yet 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. In this case, you’re using a globally unique identifier (GUID) as a unique ID. Thus, given an ID, name, and phone number, you know which customer you’re dealing with, and so you have a perfectly valid abstraction of a customer within your application.

You can then use this abstract representation of a customer from within your code. To do this, you use data binding to link the object to a form. Before proceeding, be sure to build the project. Now click the Data image from book Show Data Sources menu option to open the Data Sources window. Then click 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 the Next button, and then select your Customer class, as shown in Figure 4-11.

image from book
Figure 4-11

Finish the wizard, and the Customer class should be displayed as an available data source, as shown in Figure 4-12, if you are working in Design mode.

image from book
Figure 4-12

Click on Customer in the 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 should look something like the dialog shown in Figure 4-13.

image from book
Figure 4-13

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 code:

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

You’re using the ability of Windows Forms to data bind to a property on an object. You’ll learn more about data binding in Chapter 15. 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’ll see a display like the one shown in Figure 4-14.

image from book
Figure 4-14

Here, you’ve displayed the pre-generated ID value, and have entered values for Name and Phone directly into the form.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net