30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 Files and Streams 783<br />

(The user must click Next Record <strong>to</strong> view the first record after opening the file.) Lines<br />

68–69 call method Deserialize of the BinaryFormatter object <strong>to</strong> read the next<br />

record. Method Deserialize reads the data and casts the result <strong>to</strong> a CRecord—this<br />

cast is necessary, because Deserialize returns a reference <strong>to</strong> an instance of class<br />

Object (not of BinaryFormatter). Lines 72–79 then display the CRecord values in<br />

the TextBoxes. When method Deserialize attempts <strong>to</strong> deserialize a record that does<br />

not exist in the file (i.e., the program has displayed all file records), the method throws a<br />

SerializationException. The Catch block (defined in line 82) that handles this<br />

exception closes the FileStream object (line 84) and notifies the user that there are no<br />

more records (lines 92–93).<br />

To retrieve data sequentially from a file, programs normally start from the beginning<br />

of the file, reading data consecutively until the desired data are found. It sometimes is necessary<br />

<strong>to</strong> process a file sequentially several times (from the beginning of the file) during the<br />

execution of a program. A FileStream object can reposition its file-position pointer<br />

(which contains the byte number of the next byte <strong>to</strong> be read from or written <strong>to</strong> the file) <strong>to</strong><br />

any position in the file—we show this feature when we introduce random-access file-processing<br />

applications. When a FileStream object is opened, its file-position pointer is set<br />

<strong>to</strong> zero (i.e., the beginning of the file)<br />

Performance Tip 17.3<br />

It is time-consuming <strong>to</strong> close and reopen a file for the purpose of moving the file-position<br />

pointer <strong>to</strong> the file’s beginning. Doing so frequently could slow program performance. 17.3<br />

We now present a more substantial program that builds on the the concepts employed<br />

in Fig. 17.11. Class FrmCreditInquiry (Fig. 17.12) is a credit-inquiry program that<br />

enables a credit manager <strong>to</strong> display account information for those cus<strong>to</strong>mers with credit<br />

balances (i.e., cus<strong>to</strong>mers <strong>to</strong> whom the company owes money), zero balances (i.e., cus<strong>to</strong>mers<br />

who do not owe the company money) and debit balances (i.e., cus<strong>to</strong>mers who owe<br />

the company money for previously received goods and services). Note that line 18 declares<br />

a RichTextBox that will display the account information. RichTextBoxes provide<br />

more functionality than do regular TextBoxes—for example, RichTextBoxes offers<br />

method Find for searching individual Strings and method LoadFile for displaying<br />

file contents. Class RichTextBox does not inherit from class TextBox; rather, both<br />

classes inherit directly from MustInherit class System.Windows.Forms.Text-<br />

BoxBase. We use a RichTextBox in this example, because, by default, a Rich-<br />

TextBox displays multiple lines of text, whereas a regular TextBox displays only one.<br />

Alternatively, we could have specified multiple lines of text for a TextBox object by setting<br />

its Multiline property <strong>to</strong> True.<br />

1 ' Fig. 17.12: CreditInquiry.vb<br />

2 ' Read a file sequentially and display contents based on account<br />

3 ' type specified by user (credit, debit or zero balances).<br />

4<br />

5 ' <strong>Visual</strong> <strong>Basic</strong> namespaces<br />

6 Imports System.IO<br />

Fig. 17.12 FrmCreditInquiry class is a program that displays credit inquiries<br />

(part 1 of 7).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!