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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 17 Files and Streams 811<br />

151<br />

152 Return False ' failure<br />

153 End Try<br />

154<br />

155 Return True ' success<br />

156 End Sub ' AddRecord<br />

157<br />

158 End Class ' CTransaction<br />

Fig. 17.18 CTransaction class handles record transactions for the transactionprocessor<br />

case study (part 4 of 4).<br />

Class CTransaction contains methods OpenFile, GetRecord and Add-<br />

Record. Method OpenFile (lines 26–75) uses Const FileMode.OpenOrCreate<br />

(line 32) <strong>to</strong> create a FileStream object from either an existing file or one not yet created.<br />

Lines 35–38 use this FileStream <strong>to</strong> create BinaryReader and BinaryWriter<br />

objects for reading and writing bytes <strong>to</strong> the file. If the file is new, lines 54–64 populate the<br />

FileStream object with empty records. Students might recall that we used these techniques<br />

in Section 17.8.<br />

Method GetRecord (lines 78–128) returns the record associated with the accountnumber<br />

parameter. Lines 85–86 instantiate a CRandomAccessRecord object that will<br />

s<strong>to</strong>re the file data. If the account parameter is valid, lines 103–104 call method Seek of the<br />

FileStream object, which uses the parameter <strong>to</strong> determine the position of the specified<br />

record in the file. Lines 107–110 then call methods ReadInt32, ReadString and<br />

ReadDouble of the BinaryReader object <strong>to</strong> s<strong>to</strong>re the file data in the CRandomAccessRecord<br />

object. Line 113 returns the CRandomAccessRecord object. We used<br />

these techniques in Section 17.10.<br />

Method AddRecord (lines 131–156) inserts a record in<strong>to</strong> the file. Lines 138–139 call<br />

method Seek of the FileStream object, which uses the account-number parameter <strong>to</strong><br />

locate the position which <strong>to</strong> insert the record in the file. Lines 142–145 call the overloaded<br />

Write methods of the BinaryWriter object <strong>to</strong> write the CRandomAccessRecord<br />

object’s data <strong>to</strong> the file. We used these techniques in Section 17.9. Note that, if an error<br />

occurs when adding the record (i.e., either the FileStream or the BinaryWriter<br />

throws an IOException), lines 149–152 notify the user of the error and return False<br />

(failure).<br />

Transaction-Processor GUI<br />

The GUI for this program consists of a window containing internal frames (an MDI). Class<br />

FrmTransactionProcessor (Fig. 17.19) is the parent window, which acts as the<br />

driver for the application and displays one of its children windows—an object of type<br />

FrmStartDialog (Fig. 17.20), FrmNewDialog (Fig. 17.21), FrmUpdateDialog<br />

(Fig. 17.22) or FrmDeleteDialog (Fig. 17.23). FrmStartDialog allows the user <strong>to</strong><br />

open a file containing account information and provides access <strong>to</strong> the FrmNewDialog,<br />

FrmUpdateDialog and FrmDeleteDialog internal frames. These frames allow us-

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

Saved successfully!

Ooh no, something went wrong!