The Write Stuff
Let’s review write operations in legacy Visual Basic (before .NET). It was a simple process:
- Open a filename.
- Supply the mode.
- Give the filehandle a number.
After performing these steps, you could use that filehandle number to export a line of text to a file. Often, you could take the variable representing text box data into the file as a comma-delimited record. That’s right—you could concatenate multiple variables into a comma-delimited line of text that represented an actual record. Students could view their files and see the impact of things such as missing fields, or other kinds of creepy issues.
Life was certainly easy. In fact, once open, that filehandle was accessible from multiple spots in your apps. The form load event could open the file and then the button processing could stuff the file. Later, the application’s end event would close files easily. (Ah, those were the days.) Actually, things just got a bit more complex, and few authors and online help writers provided the examples we like. There are StreamWriters and all kinds of stuff. No longer are filehandle numbers enough—you need variables. And few examples existed of opening a file and stuffing the file in other parts of the application.
My Attendance application, which we’ll study in this article, takes us almost all the way back to those halcyon days of yore. Let’s examine what the Attendance app does. First, we’ll address using those filehandles as numbers. Easy to use, easy to confuse when opening several files. (Which filehandle is the read? #2 or #3?) We begin by creating file variables that may help coding clarity:
Dim myfilename As String Dim myfilewriter As StreamWriter Dim myoutfile As FileStream Dim chkboxcoll As New Collection()
We need a FileStream object to output our file. A FileStream is simply a stream of bytes. This is somewhat comparable to declaring mode of output. With this class, we declare the filename, the mode, and the access type (write, in this case). Once we have a valid object of FileStream (which I derive and cleverly call myoutfile), we can set up the filehandle. This time, instead of a number, we have a variable name that I call myfilewriter, a derived instance of StreamWriter. Every time I call myfilewriter, the FileStream of bytes goes rolling to the filename with write access.
Of course, these variables are inane and in no way help the coder. The sample code you just saw lacks comments. As a (past or present) Visual Basic .NET student, you’ll want to rewrite this code and improve it, making it more efficient, more supportable—just better.
So we have an open file, ready to receive bytes. How do you use this file later in the application? While processing a button_click, I inspect the three student entry check boxes:
If objchkboxcoll.Checked = True Then MessageBox.Show("doh! " & objchkboxcoll.Text & " is absent!", "Absent") myfilewriter.WriteLine(objchkboxcoll.Text) End If
If the check box is checked, I show a message box and then invoke the WriteLine method for my derived myfilewriter object. It’s as simple as that. And as you see from my concatenation used with the message box, you can build up a line for the WriteLine method that includes the text from several interface elements.
With one button’s event, I open the file; with another, I write a line of text to it—same as in the old days of Visual Basic. This time, however, I have the chance to use a meaningful variable name—if I choose to do so. Certainly by now you want to work with this code, if only to change the variable name to CamelHumpCapitalization. How can you resist correcting this code?
With .NET, you can have effortless text file operations, and you can have more understandable code.
Our example application reads from an existing file of student names. In the compact world, it’s not a bad thing to read from files and have users confirm input rather than typing their input. Restaurant waiters do better when checking off known entrees, rather than trying to type them into an ordering system. Handwriting recognition isn’t bad, but data validation can mean extra steps. Imagine the benefits of encoding the menu and simply displaying each course with check boxes.
How do you read a file in .NET’s take on the text file?