Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

A “REAL” CLASS 367

contact.EMail = txtEMail.Text contact.URL = txtURL.Text

If adding Then ListBox1.Items.Add(contact)

Else

ListBox1.Items(currentContact) = contact ListBox1.Items.RemoveAt(currentContact) ListBox1.Items.Add(contact)

End If End Sub

The SaveContact() subroutine uses the adding variable to distinguish between an add and an edit operation, and either adds the new record to the ListBox control or replaces the current record in the ListBox with the values on the various controls.

The last step is the serialization and deserialization of the items on the ListBox control. Serialization is the process of storing the object to a disk file, and deserialization is the opposite process. To serialize objects, we first store them into an ArrayList object. The ArrayList object is a dynamic array that stores objects; it can be serialized as a whole. Likewise, the disk file is deserialized as an ArrayList; then each element of the ArrayList is moved to the Items collection of the ListBox control. The ArrayList object is discussed in detail in Chapter 11, along with the techniques for serializing and deserializing its elements.

The ClassContacts application demonstrates how to use classes to implement custom objects; it’s also a demonstration of how the ListBox control should be used. In Chapter 6, when we explored the ListBox control, you saw examples of storing strings to this control. To make the most of the ListBox control, use it to save objects in its Items collection. In addition to storing data, the ListBox control is also a fine navigational tool, as long as it’s sorted and the objects you store to the control provide a custom ToString method that returns a string identifying the object.

Encapsulation and Abstraction

As you have seen, developing a new custom class with VB is a straightforward process. In effect, it’s very similar to writing regular VB code. So, why build classes in the first place? One of the advantages of using classes is that their functionality is cast in iron; other developers can use it, but they can’t break it. You can think of classes as black boxes, and this is what programmers call encapsulation. The String data type encapsulates a lot of functionality and exposes it through its properties and methods (the Length property, or the Split method, for example). The functionality is added to the class and is available to all applications that make use of the String class.

If you have a set of utility functions and you use them in several of your projects, you’re already familiar with the following scenario. You modify one function for the needs of a specific application, then you modify another function to suit another application, and as you go along you break applications that used to work with the old versions of the functions. If you place all your custom string-manipulation functions in a class, you’ll encapsulate their functionality.

Encapsulation doesn’t mean that the class must be used “as is.” As you will see in the following section, it is possible (and desirable) to modify a member. In short, you can create new classes based on existing ones and add new members, or revise existing members. The old applications will work

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com