Re: This should never work but it does. Why?
From: Brian Cryer (brianc_at_127.0.0.1.activesol.co.uk)
Date: 12/21/04
- Next message: anonymous_at_discussions.microsoft.com: "Re: Find IP Address"
- Previous message: Rulin Hong: "RE: How to make this better and faster?"
- In reply to: TVR Fan: "This should never work but it does. Why?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 21 Dec 2004 15:13:59 -0000
"TVR Fan" <TVRFan@discussions.microsoft.com> wrote in message
news:DD550152-22B2-465D-9001-D88DAE4AB5A2@microsoft.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
- Next message: anonymous_at_discussions.microsoft.com: "Re: Find IP Address"
- Previous message: Rulin Hong: "RE: How to make this better and faster?"
- In reply to: TVR Fan: "This should never work but it does. Why?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|