Home > Articles

  • Print
  • + Share This
This chapter is from the book

A vbFile Class

File handling in VB is for the most part awkward and primitive for historical reasons. The statements for opening files have this form.

f = FreeFile
Open "file.txt" for Input as #f

And those for reading data from files have this form.

Input #f, s
Line Input #f, sLine

There is no simple statement for checking for the existence of a file, and the file rename and delete have counterintuitive names.

Exists = len(dir$(filename))>0          'file exists
Name file1 as file2                     'Rename file
Kill filename                           'Delete file

None of these statements are at all object oriented. There ought to be objects that encapsulate some of this awkwardness and keep the file handles suitably hidden.

VB6 introduced the Scripting.FileSystemObject as a way to handle files in a presumably more object-oriented way. However, these objects are not fully realized and a bit difficult to use. Thus, we might do well to create our own vbFile object with convenient methods. These methods could include the following.

Public Function OpenForRead(Filename As String) As Boolean
Public Function fEof() As Boolean
Public Function readLine() As String
Public Function readToken() As String
Public Sub closeFile()
Public Function exists() As Boolean
Public Function delete() As Boolean
Public Function OpenForWrite(fname As String) As Boolean
Public Sub writeText(s As String)
Public Sub writeLine(s As String)
Public Sub setFilename(fname As String)
Public Function getFilename() As String

A typical implementation of a few of these methods includes the following.

Public Function OpenForRead(Filename As String) As Boolean
 'open file for reading
 f = FreeFile                'get a free handle
 File_name = Filename        'save the filename

 On Error GoTo nofile       'trap errors
 Open Filename For Input As #f
    opened = True           'set true if open successful
oexit:
    OpenForRead = opened    'return to caller
Exit Function
'--error handling--
nofile:
  end_file = True           'set end of file flag
  errDesc = Err.Description 'save error messae
  opened = False            'no file open
  Resume oexit              'and resume
End Function
'------
Public Function fEof() As Boolean
 'return end of file
 If opened Then
  fEof = EOF(f)
 Else
  fEof = True    'if not opened then end file is true
 End If
End Function
'------
Public Function readLine() As String
 Dim s As String
 'read one line from a text file
 If opened Then
   Line Input #f, s
   readLine = s
 Else
   readLine = ""
 End If
End Function

With these useful methods, we can write a simple program to read a file and display it in a list box.

Dim fl As New vbFile
cDlg.ShowOpen   'use common dialog open

fl.OpenForRead cDlg.Filename
'read in up to end of file
sline = fl.readLine
While Not fl.fEof
  lsFiles.AddItem sline
  sline = fl.readLine
Wend
fl.closeFile

Now, the implementation of this vbFile object can change as VB evolves. However, by concealing the details, we can vary the implementation in the future. We'll see another implementation of this class when we discuss VB.NET.

  • + Share This
  • 🔖 Save To Your Account