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.

810 Files and Streams Chapter 17<br />

99 ' get data from file if account is valid<br />

100 Else<br />

101<br />

102 ' locate position in file where record exists<br />

103 file.Seek( _<br />

104 (accountNumber - 1) * CRandomAccessRecord.SIZE, 0)<br />

105<br />

106 ' read data from record<br />

107 record.Account = binaryInput.ReadInt32()<br />

108 record.FirstName = binaryInput.ReadString()<br />

109 record.LastName = binaryInput.ReadString()<br />

110 record.Balance = binaryInput.ReadDouble()<br />

111 End If<br />

112<br />

113 Return record<br />

114<br />

115 ' notify user of error during reading<br />

116 Catch fileException As IOException<br />

117 MessageBox.Show("Cannot read file", "Error", _<br />

118 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Error)<br />

119<br />

120 ' notify user of error in parameter mismatch<br />

121 Catch formattingException As FormatException<br />

122 MessageBox.Show("Invalid Account", "Error", _<br />

123 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Error)<br />

124<br />

125 End Try<br />

126<br />

127 Return Nothing<br />

128 End Function ' GetRecord<br />

129<br />

130 ' add record <strong>to</strong> file at position determined by accountNumber<br />

131 Public Function AddRecord(ByVal record As CRandomAccessRecord, _<br />

132 ByVal accountNumber As Integer) As Boolean<br />

133<br />

134 ' write record <strong>to</strong> file<br />

135 Try<br />

136<br />

137 ' move file-position pointer <strong>to</strong> appropriate position<br />

138 file.Seek( _<br />

139 (accountNumber - 1) * CRandomAccessRecord.SIZE, 0)<br />

140<br />

141 ' write data <strong>to</strong> file<br />

142 binaryOutput.Write(record.Account)<br />

143 binaryOutput.Write(record.FirstName)<br />

144 binaryOutput.Write(record.LastName)<br />

145 binaryOutput.Write(record.Balance)<br />

146<br />

147 ' notify user if error occurs during writing<br />

148 Catch fileException As IOException<br />

149 MessageBox.Show("Error Writing To File", "Error", _<br />

150 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Error)<br />

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

case study (part 3 of 4).

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

Saved successfully!

Ooh no, something went wrong!