By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

This should never work but it does. Why?

P: n/a
I've just fixed what should be a bug in a simple function to log a message to
a file. The thing is, the original code worked!

Protected Sub LogMessage(ByVal filename As String, ByVal message As
String)
Dim fileNum As Integer

fileNum = FreeFile()
FileSystem.FileOpen(FreeFile(), filename, OpenMode.Append)
FileSystem.PrintLine(fileNum, DateTime.Now.ToString + " - " + message)
FileSystem.FileClose(fileNum)
End Sub

I'm opening the file with one number and then using a different file number
to write the data! Surely the PrintLine and the FileClose should fail. But
the application works and logs mesages to the log file with no problems.

Can anyone explain?

--- Al.
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"TVR Fan" <TV****@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com...
I've just fixed what should be a bug in a simple function to log a message to a file. The thing is, the original code worked!

Protected Sub LogMessage(ByVal filename As String, ByVal message As
String)
Dim fileNum As Integer

fileNum = FreeFile()
FileSystem.FileOpen(FreeFile(), filename, OpenMode.Append)
FileSystem.PrintLine(fileNum, DateTime.Now.ToString + " - " + message) FileSystem.FileClose(fileNum)
End Sub

I'm opening the file with one number and then using a different file number to write the data! Surely the PrintLine and the FileClose should fail. But
the application works and logs mesages to the log file with no problems.

Can anyone explain?

--- Al.


The original worked because you are using the same file number.

FreeFile returns the next free file number that is not currently in use. The
thing to remember is that until you open a file using that file number, that
file number isn't in use. Therefore the second call to FreeFile returned the
same file number as the first.

However I do agree that it is better style to use:

fileNum = FreeFile()
FileSystem.FileOpen(fileNum ....

hope this helps,

Brian Cryer

www.cryer.co.uk/brian
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.