program. The specific example is a "FileGuard" class that guarantees that a
file you open gets closed, no matter what else happens.
How it works: whenever you declare a class in LotusScript, you can define a
Delete method that executes when an object of that class is deallocated. This
happens whenever the memory is freed, whether it's because the object has gone
out of scope from the function it was declared in, or because the script
aborted with an error, or by Ctrl+Break.
The simple class below is useful when doing file I/O to make sure that you
don't leave a file open if the user aborts.
Note: you must open a file using the returned file handle from the Handle
method, before you allocate another instance of FileGuard, or else they will
get the same file number. Since Freefile returns the number of the first file
handle that's not yet in use, calling it twice without using the result of the
first call to open a file, returns the same value again.
filenumber As Integer
isopen As Integer
Public Property Get Handle
Handle = filenumber
filenumber = Freefile( )
isopen = True
If isopen Then
On Error Resume Next
isopen = False
Then when you're ready to open a file:
' Keep the next three statements together
Dim FG1 As New FileGuard
outfile% = FG1.Handle
Open "C:\TEMP\DUMP.TXT" For Output As outfile%
Dim FG2 As New FileGuard
infile% = FG2.Handle
Open "\\RAGAMUFFIN\ARGH\TWIST.DAT" For Input As infile%
FG2.Close ' in either order.
This was first published in November 2000