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

528 Chapter 11 STORING DATA IN COLLECTIONS

<item id=”ref-7” xsi:type=”SOAP-ENC:string”>ABANDON</item> <item id=”ref-8” xsi:type=”SOAP-ENC:string”>ABANDONED</item> <item id=”ref-9” xsi:type=”SOAP-ENC:string”>ABANDONING</item>

The corresponding counts are:

<item xsi:type=”xsd:int”>2064</item> <item xsi:type=”xsd:int”>1</item> <item xsi:type=”xsd:int”>5</item> <item xsi:type=”xsd:int”>10</item> <item xsi:type=”xsd:int”>2</item>

Most of us shouldn’t really care how the Serialize method stores the data to the XML file, or to the binary file, as long as the Deserialize method can read them back into and load them into an object so that we won’t have to write code to parse this file.

Summary

The Collection classes we explored in this chapter are just a few among the 3,500 classes of the

.NET Framework, but they’re certainly among the most useful ones. Just about any application that needs to persist data between sessions can benefit from these classes. Even the humble array has been totally revamped; it can now sort and search its elements. The ArrayList is a dynamic array, while the HashTable is offers many of the advantages of an ArrayList plus the capability to access its elements by meaningful keys.

All the collections support custom sorting and searching. If the collection contains objects or custom structures, you can provide simple custom functions to enable the comparison and sorting of the collection’s elements. Not only that, but you can provide multiple comparers and use each one to sort the collection in many different ways, as needed.

The last class we explored in this chapter was the Serialization class, which basically enables you to persist complicated objects with a single function call. You saw examples of using the members of the Serialization class with an ArrayList, but this is probably the most efficient way to use the Serialization class. An ArrayList may can be populated with objects of any type, so you can store your custom objects to an ArrayList and then persist the entire collection to a file with a few simple statements.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 12

Handling Strings, Characters,

and Dates

This chapter is a formal discussion of the .NET Framework’s stringand date-manipulation capabilities. You have seen many of the string functions in earlier chapters, as well as many of the properties and methods of the String class. Almost every application manipulates strings, so the String and StringBuilder classes are two that you’ll use more than any other.

Previous versions of Visual Basic provided numerous functions for manipulating strings. These functions are supported by VB.NET, and not just for compatibility reasons; they’re part of the core of Visual Basic. The string-manipulation functions are still a major part of VB, while the math functions were removed from the language and placed in a special class, the System.Math class. This is a good indication of the type of processing that takes place in a typical application. You’ll write more code to manipulate strings than to do math.

Another group of functions deals with dates. The date-manipulation functions are still part of the core of the language and were not moved to a special class. Many of these functions are duplicated in the DateTime class, in the form of properties and methods. The Month() function, for example, returns the month of a date passed to the function as argument. The DateTime.Month property does the same: it returns the month of a date value. The following statements print the number of the current month:

Console.WriteLine(Month(Now()))

Console.WriteLine(Now().Month)

The first statement used the Month() function, while the second one uses the Date class’s Month method. The function Now() returns the current date, which is a value of the Date type (which is the same as the DateTime type), and that’s why we can apply the Month method to this value.

The stringand date-handling functions of Visual Basic are described in the reference “VB.NET Functions and Statements” (on the companion CD). These functions aren’t new to VB.NET anyway, and it’s important that you familiarize yourself with the built-in functions, even if you’re going to use the new classes of the .NET Framework. There are miles of VB6 code out there, and it’s more than likely that you will run into code ported from VB6 applications, or you’ll be asked to port VB6 code into .NET.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com