Results 1 to 16 of 16

Thread: [RESOLVED] Why use Freefile

  1. #1

    Thread Starter
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Resolved [RESOLVED] Why use Freefile

    Many VB programs I write only require a few files to be opened. I normally just use Open...as #1. Is there any inherent reason why I should use freefile instead?

    Cadman

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Why use Freefile

    As long as you keep track of them and you are sure that when you use a file number it isn't already in use then no you don't need to use Freefile.

    It's mainly for your coding protection and it allows the system to assign a file number that isn't in use.

    In your case, no.

    In very large programs with many open files I would say yes

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Why use Freefile

    Yes, there is a reason to use FreeFile in case file with handle #1 is still open.
    If you try to use it again VB will generate runtime error.
    On the other hand if you use FreeFile then you can be certain that your program uses available file handle.

    However, if you only open 1 file and never another until first is close then it's safe to use #1 [I guess].

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Why use Freefile

    It's a "best practice", much like using Option Explicit.

  5. #5

    Thread Starter
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Re: Why use Freefile

    However, if freefile gives a new handle number while the file is opened (for input only) I expect it will allow a 2nd instance of opening again for input only where I may prefer to have it raise an error to indicate the previous open should have been closed. I assign say #1 for inputing a certain file, #2 for its output and maybe #3 for any other supporting data. So I know then that #1 is my source and it helps to keep track of which file is which and if I do get any error I will know I have not coded the proper error handling of that particular file #.

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Why use Freefile

    Instead of Freefile or #1, #2, etc why not use constants

    Private Const InputFile = 1
    Private Const OutputFile = 2
    Private Const SupportFile = 3
    etc
    etc

    Open "file-path\input-file-name" For Input As #InputFile

    Open "file-path\output-file-name" For Output As #OutputFile

    Instead of getting all mixed up with a lot of different opinions just use the method that you feel most confortable with. It's really up to you.

    NOTE

    I do a termedous amount of coding and debugging on the fly (during run-time) and in doing so I add many lines just to test so I want to be able to just drop in a variable or such without the concern of having to pre-define everything so I usually do not use Option Explict during this time and I usually use file numbers like #121, 236, 44, 77, 106, etc etc. Mentally I can keep track of these file numbers because they are so far apart in value rather than #1, #2, #3, #4 etc. Later, after I am satified with all the changes I made I will go back and do a clean up to get everything looking nice and neat. But this is me. Someone else will differ.
    Last edited by jmsrickland; Feb 27th, 2009 at 12:11 PM.

  7. #7

    Thread Starter
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Re: Why use Freefile

    That is an excellent idea. I just wanted to see if I was missing anything regarding freefile. Of course, not closing a file for input may not damage anything but doing that is bad form/practice. VBforums is terrific, thanks.

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Why use Freefile

    Quote Originally Posted by CADman
    That is an excellent idea...
    And it probably is. However, using FreeFile is the best one by far.
    You can have (if necessary) 3 integer variables and assign value for each with FreeFile instead of hardcoding them.

    Regards.

  9. #9
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] Why use Freefile

    That doesn't work, Rhino. Calling Freefile three times in a row returns the same number every time. The handle (channel? stream?) is still free until it's used.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Why use Freefile

    I never said "in-a-row" - of course you have to use it:
    Code:
    Private Sub Command1_Click()
    Dim inputFile As Integer
    Dim outputFile As Integer
    
        inputFile = FreeFile
        Debug.Print inputFile
        Open "c:\test1.txt" For Input As #inputFile
            '...
            outputFile = FreeFile
            Debug.Print outputFile
            Open "c:\test2.txt" For Output As #outputFile
                '...
            Close #outputFile
        Close #inputFile
    
    End Sub
    I've seen projetcs with recursive calls to function that needed to open file(s) but not necessary close when function is completed.
    What are you going to do in that kind of situation? Declare const for each possible case?
    That's what FreeFile is for - dynamically give you what's available.

  11. #11
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] Why use Freefile

    It sounded like you were recommending a variation of the constant approach, using variables instead.

    The example you posted goes without saying; of course you have to store freefile's return value to a variable. I don't think I've ever seen anyone use freefile without storing the return value to a variable.

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Why use Freefile

    You need to keep in mind that the OP was talking about only a very few files; 1, 2, 3, ,4, maybe 5 at a time. For that, constants are not a bad idea but alot of files....that's another story.

  13. #13
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Why use Freefile

    VB6 "file numbers" are actually like an index into an array of the actual file handles. There is even a slight difference between those returned by FreeFile(0), file numbers 1-255, and FreeFile(1), file numbers 256-511:
    file number

    Number used in the Open statement to open a file. Use file numbers in the range 1–255, inclusive, for files not accessible to other applications. Use file numbers in the range 256–511 for files accessible from other applications.
    Another consideration is that the VB6 Close statement is not synchronous. It takes some time for the final buffer to be written to an output file, and until that write completes VB6 does not actually close the file and release its handle (and thus its file number).

    So...

    If you process a sequence of files one by one in a loop and use a hard-coded file number it is possible for an exception to be thrown on the second, third, etc. Open using that same file number.

    Always use FreeFile() calls to get a file number, just as you should always use Option Explicit as already suggested.

  14. #14
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [RESOLVED] Why use Freefile

    Quote Originally Posted by dilettante
    VB6 "file numbers" are actually like an index into an array of the actual file handles. There is even a slight difference between those returned by FreeFile(0), file numbers 1-255, and FreeFile(1), file numbers 256-511:


    Another consideration is that the VB6 Close statement is not synchronous. It takes some time for the final buffer to be written to an output file, and until that write completes VB6 does not actually close the file and release its handle (and thus its file number).

    So...

    If you process a sequence of files one by one in a loop and use a hard-coded file number it is possible for an exception to be thrown on the second, third, etc. Open using that same file number.

    Always use FreeFile() calls to get a file number, just as you should always use Option Explicit as already suggested.
    where did you find your information on the asynchronous nature of the close?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  15. #15
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Why use Freefile

    Quote Originally Posted by dilettante
    Another consideration is that the VB6 Close statement is not synchronous. It takes some time for the final buffer to be written to an output file, and until that write completes VB6 does not actually close the file and release its handle (and thus its file number).
    Agree that the Close statement takes some time to finish but I have the same question as Lord Orwell.
    In sequencial processing, perhaps the next line of code will not be executed until the Close statement is finished.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  16. #16
    Member lmstearn's Avatar
    Join Date
    Dec 2016
    Location
    Australia
    Posts
    58

    Re: [RESOLVED] Why use Freefile

    Quote Originally Posted by Lord Orwell View Post
    where did you find your information on the asynchronous nature of the close?
    (In gardening there's dead heading- just honing these skills in dead threading):
    The logs in Process Monitor show how these events are generated through time.

    Came to this thread because of calling FreeFile without the brackets. Oops.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width