tags:

views:

73

answers:

4

I just wrote this function to read a series of email addresses from a linebreak-delimited text file. And it does work, but that's not my question.

Function GetEmailArray(FileName As String) As String()
    Dim TempArr() As String
    Dim i As Integer

    Open FileName For Input Access Read As #1
    Do While Not (EOF(1))
        i = i + 1
        ReDim Preserve TempArr(i + 1)
        Line Input #1, TempArr(i + 1)
        Debug.Print TempArr(i + 1)
    Loop
    Close #1
    GetEmailArray = TempArr
End Function

Reading this, I would expect this to:

  1. Read the first line, store it in TempArr(1)
  2. Loop
  3. Read the first line AGAIN, store it in TempArr(2)
  4. Loop
  5. Etc

I just can't figure out how the while loop goes to the next line in the text file.

+1  A: 

I believe the magic is happening on Line Input #1, TempArr(i+1). It reads from file handle #1 to TempArr(i+1). When it gets to the end of the file, EOF(1) will evaluate to true and terminate the while loop. How does the system know which is the next line? That's handled under the hood for you, by Line Input.

FrustratedWithFormsDesigner
+2  A: 

You're holding a handle (#1) to the file from the point you call Open, until you call Close. Behind the scenes (on the O/S level), the handle retains a current file position as part of the file descriptor.

Pontus Gagge
This is the first time I've used Open(). Yesterday, I was trying to write this by choosing a driver, setting up a connection string, writing a schema.ini file, creating a recordset using an ADODB connection, bleh. This is much easier. Thanks for the info.
PowerUser
A: 

The Line Input function is altering the current line. Pretty tacky stuff.

Rushyo
Does Line Input really *alter* anything or does it just read from a file and store into a variable? Well, I guess it alters the target variable, in the sense that it populates it with data...
FrustratedWithFormsDesigner
Tacky? I've never heard that word used before with code. Please elaborate?
PowerUser
@Frustrated, By invoking "Access Read", I'm opening the file for Read-Only access.
PowerUser
@PowerUser: Yeah, when was the last time you saw code with rhinestones and leopard-print leggings and 10 pounds of pseudo-gold bling?
FrustratedWithFormsDesigner
@PowerUser: That was my point. The *source* of the lines (the file) is **not** altered by reading it.
FrustratedWithFormsDesigner
@FrustratedWithFormsDesigner It alters the current line in the sense that it alters the reference to the current line. Kinda like you tend to refer to objects in C# rather than references to objects.
Rushyo
"Tacky? I've never heard that word used before with code. Please elaborate?" Low quality. Cheap. Most obvious approach with minimal thought to the actual consequences. That sums up VBA as a whole, mind you.
Rushyo
@Rushyo: Thanks, that's clearer (but your initial answer wasn't).
FrustratedWithFormsDesigner
@Rushyo: Low quality? Well, it does have an error handler, but I left that out of my post since it wasn't relevant to my question. And I'm not including any data validation code since it isn't important here. Am I missing anything?
PowerUser
@PowerUser It wasn't a dig at your code. Just VBA.
Rushyo
@FrustratedWithFormsDesigner Perhaps an overzealous attempt at efficiency, but again you won't see anyone referring to 'a reference to an object' in C# where 'object' will suffice. That includes in Microsoft documentation.
Rushyo
+2  A: 

Your step 3 should be:

3. Read the next line from the file into TempArr(i + 1)

So you do not read in the first line again, the line input statement reads the line, and places the file-position on the next line.

GvS
I thought Step 3 was always "Profit!!"
FrustratedWithFormsDesigner
This is VBA ;-)
GvS