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

568 Chapter 12 HANDLING STRINGS, CHARACTERS, AND DATES

The following code outlines the code for timing a group of statements:

Dim TStart, TEnd As Date

Dim Duration As TimeSpan TStart = Now()

{ enter here the statements to be timed } TEnd = Now()

Duration = TEnd.Subtract(TStart)

Console.Write(“Statements took “ & TS.TotalSeconds & “ seconds to execute.”)

In the StringReversal project, earlier in this chapter, we used these statements to time the operation of reversing a String (or StringBuilder) variable’s characters. In this project, we timed the block of statements that reverse the string in-place. The statements we timed form a loop, which goes through the characters in the string. If you divide the duration of the operation by the number of characters processed, you will find out how long it takes to process each character.

The timing of operations is called benchmarking, and it’s one of the most difficult aspects of computing, because of the many factors that may affect a specific operation. In many cases, these factors aren’t obvious, and different companies will come up with different benchmarks for the same software. The technique discussed in this chapter is adequate for comparing two alternate ways of coding the same operation, but an actual benchmark is far more complicated.

Summary

In this chapter, you learned about some of the most interesting base data types. Strings, characters, and date/time values are some of the richest data types, as they encapsulate a lot of functionality. For the first time, you have at your disposal methods to simplify the processing of characters—like figuring out whether a character is white space, a letter, a digit, or even a separator. The TimeSpan class encapsulates all the functionality you need to manipulate time differences, and it gives you the tools for timing at a very fine level. The StringBuilder class, finally, provides the fastest stringmanipulation methods VB has ever provided.

This chapter ends our discussion of the fundamental classes of the .NET Framework. This doesn’t mean that the other classes are less important. There’s no way to cover all of the Framework’s classes in a single book, especially in a book that teaches programming with VB. I’ve chosen the classes you’ll be using most often in your applications and presented them in detail, rather than attempting to mention just the basics of many classes.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 13

Working with Folders and Files

Files have always been an important aspect of programming. We use files to store data, and in many cases we have to manipulate files and folders from within applications. I need not give examples: just about any application that allows user input must store its data to a file (or multiple files) for later retrieval—databases excluded, of course.

Manipulating files and folders is quite common too. Organizing files into folders and processing files en masse are two typical examples. I recently ran into a few Web-related tasks, that are worth mentioning here. A program for placing watermarks on pictures was the first. A watermark is a graphic that’s placed over an image to indicate its origin. The watermark is very transparent, so that it doesn’t obscure the image, but it makes the image unusable on any site other than the original site. You will see in Chapter 14 how to place a semitransparent graphic on top of an image, and with the help of the information in this chapter, you’ll be able to scan a folder with thousands of images and automate the process of watermarking the images.

Another example has to do with matching filenames to values stored in a database. Product images are usually named after the product’s ID, and they’re stored in separate files. There’s a need for programs to match product IDs to images, find out whether there’s an image for a specific product in the database, or simply move the image files around (store the images for different product categories into different folders and so on).

You may even need to initiate some action when a file in a specific folder is created, edited, or deleted. For example, you can process files as soon as they’re uploaded to a server or copied to a specific folder. There are better ways to automate the workflow, but I’m not going to elaborate on this. The fact is that many programmers still use these methods. They work, and you may have to not only act on files, but also initiate other actions from within your applications based on changes in the file system. In the last section, you’ll learn about the FileSystemWatcher, a component that can detect changes in the file system and notify your application about these changes through events.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com