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


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



Relevant Pages

  • Re: [EGN] Variable hoisting
    ... Re: The Data Quality Act ... In the process I found a SERIOUS bug in the C code. ... CS> The allocated string has no room for the trailing nul. ... An mature adult programmer would have taken it for what ...
    (comp.programming)
  • Re: Comment on trim string function please
    ... I think you have a bug. ... but does it fly past the terminating null character? ... initial space scan) then this writes outside the string. ... ASIDE FROM THE CAST "ISSUES", ...
    (comp.lang.c)
  • How To Add Users GUID To An Account Verification E-mail
    ... String userName, String password, MailDefinition mailDefinition, String ... defaultSubject, String defaultBody, OnSendingMailDelegate ... Protected Sub CreateUserWizard1_SendingMail(ByVal sender As Object, ... 'Replace placeholder for the verification URL ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Weekly Python Patch/Bug Summary
    ... Patch / Bug Summary ... most missing from Windows distribution ... http://python.org/sf/1541585 closed by gbrandl ... Compiler command percent-sign causes format string error ...
    (comp.lang.python)
  • Re: A critique of test-first...
    ... one nasty bug. ... Now we have a string that has been output to stdout. ... Maybe we don't need to exercise the whole compiler, ... Well, again ex hypothesi, I wrote it ...
    (comp.programming)