Closing Up
As with legacy Visual Basic, no open file must stay open forever! It’s important to close a file completely. In Visual Basic .NET, closing files is done as easily as with legacy Visual Basic. But first, I bet you’re wondering what that collection is that I used.
Many coders loved control arrays in legacy Visual Basic. It was nice to step through a collection of controls on your form and act on them appropriately. Control arrays were an accommodation, part of legacy Visual Basic’s stance to keep you from tough stuff. Visual Basic .NET is a step up, using mainstream programming fundamentals to allow Visual Basic to scale as never before. Remember this line?
Dim chkboxcoll As New Collection()
By declaring a collection of check boxes, I have my control array—only without the clunk of kludge that implementing control arrays brings. I can step through my objects cleanly:
For Each objchkboxcoll As CheckBox In (chkboxcoll)
Ain’t it grand? I’ve implemented a sophisticated data structure defined with precision, and I get to use the For Each directive—cooler than some clunk of a traditional For...next statement. Honestly, .NET seems complicated, but it’s actually easy once you try it out. I recently reinstalled Visual Basic 4 on the XP virtual machine I have on my Windows Vista system. I respect what Visual Basic did to simplify Windows coding, but before long those training wheels only slowed me down. Try Visual Basic .NET.
It’s time to Application.Exit the program. What now?
Don’t leave a file open. Close it to ensure that all buffers are flushed. It’s as simple as using the variable name you used:
myfilewriter.Close() myoutfile.Close()
And that’s it.
What do you think? Is it possible to use Visual Basic .NET with legacy Visual Basic speed when it comes to text files? I think so. In fact, I’m forgetting the My object now that I see how well the .NET Framework and .NET Compact Framework support text files. By mapping the new concepts to the old, I have all the speed of development that I had with the old ways, but gain something I never had: code that was more mainstream-supportable. The For Each and the use of a variable filehandle replicates what I’ve done in PERL and other languages. It just makes sense. Using collections is an improvement, I think, over control arrays.
In other words, in Visual Basic .NET you can do everything you’ve done in legacy Visual Basic. You may even do those things faster, and you’ll have many more platforms for your code—and more potential customers. A lot of smart phones out there need utilities. Microsoft just announced its management server that will take on RIM’s Blackberry Enterprise Server. Imagine order pickers using smart devices to read punch lists and the scan in the parts. All of that data can merge into corporate systems, thanks to your coding. Your managers will love your expense checker that runs on a smart phone—and then posts the entries to the expense book application, which runs on a server, a web server, or even a Pocket PC. (Some people find that dinky screen limiting. I think it’s exhilarating because it enables us coders to focus on the application, instead of silly interface elements like logos, colors, etc.