Wrox Programmer Forums
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 20th, 2011, 03:38 PM
Registered User
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch13 clsRandomAccess WriteOneRecord question

I see that in the try block we have two tests before we try to write:

if (myFile != null && bw != null){
   // Seek
   bw = new BinaryWriter(myFile);
   // Write stuff
My question is, why are we testing the prior existence of bw if we intend to create a new BinaryWriter a few lines later?
Old April 20th, 2011, 04:00 PM
Friend of Wrox
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Binary writer

Truth be told, I only vaguely remember why the technical editor wanted that check. I think it was because checking bw insures that Open() was called first and then Create() if the file has not been opened earlier in a write sequence. Since it is always a good idea to give resources, like file handles, back to the op system when you're done, calling WriteOneRecord() without opening the file for writing would throw an exception. If both members are non-null, it means that the file is open and can be used for writing. I think what the editor had in mind is a loop where, once the file is open, you can spin through a loop and write multiple records without having to open and close the file before each write.

Personally, I always prefer calling Open(), then WriteOneRecord(), and finally Close() to make sure I tie up the resources only as long as I need to. This also means that I tend to write only one record at a time, which is usually the case when you are adding or editing data.

For those of you trying to follow along, see page 374 and also 376.
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)

Similar Threads
Thread Thread Starter Forum Replies Last Post
ch13 more errors solved sporik BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 December 4th, 2009 02:04 PM
Ch13 Case Study 2 alecwood BOOK: Beginning Access 2003 VBA 0 October 18th, 2007 05:40 AM
Ch13 EditingData.aspx mahir BOOK: Beginning ASP.NET 1.0 8 April 28th, 2004 03:25 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.