Results 1 to 6 of 6

Thread: Is this the best way?

  1. #1

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610
    I am trying to save records that are in a binary file to a string array. The code below works but leads me to that there could be a better way to get number of strings in the file.

    Is it safe just to put in 16 instead of Len(ObjectID(0)) or is this not good programming practice.

    Code:
    Public Sub File2Query(FilePath As String)
    
    Dim TempDataFile As Integer
    Dim ObjectID() As String * 16
    Dim i As Integer
    
        ReDim ObjectID(1)
        ObjectID(0) = ""
    
        ReDim ObjectID(FileLen(FilePath) / Len(ObjectID(0)))
        
        TempDataFile = FreeFile
        Open FilePath For Binary As #TempDataFile
    
        Get #TempDataFile, , ObjectID
    
        Close #TempDataFile
    
    End Function
    Thanks
    This space for rent...

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well since you have to call ReDim twice it's not really effesiant.
    You could of course put in 16 instead of Len(ObjectID(0)) but then you have to make changes at different places in your code if you wanted to change the length of the strings.
    I think the best thing is to declare a constant with the string length:
    Code:
    Public Sub File2Query(FilePath As String)
        Const STRINGLEN As Integer = 16
        Dim TempDataFile As Integer
        Dim ObjectID() As String * STRINGLEN
        Dim i As Integer
    
        ReDim ObjectID(FileLen(FilePath) / STRINGLEN)
        
        TempDataFile = FreeFile
        Open FilePath For Binary As #TempDataFile
    
        Get #TempDataFile, , ObjectID
    
        Close #TempDataFile
    End Function
    This constant could of course be declared in the General Declaration section as well if you use it in some other procedure.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Oh... and an other thing, the LOF function is slightly faster than the FileLen function.
    And also you could use \ operator instead of the / operator
    Code:
    Public Sub File2Query(FilePath As String)
        Const STRINGLEN As Integer = 16
        Dim TempDataFile As Integer
        Dim ObjectID() As String * STRINGLEN
        Dim i As Integer
        
        TempDataFile = FreeFile
        Open FilePath For Binary As #TempDataFile
          ReDim ObjectID(LOF(TempDataFile) \ STRINGLEN)
          Get #TempDataFile, , ObjectID
        Close #TempDataFile
    End Function

  4. #4

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Huh?

    Originally posted by Joacim Andersson
    also you could use \ operator instead of the / operator
    Huh? Whats the difference? I thought you always use / for division.
    This space for rent...

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Nope the \ operator always returns an Integer and is much faster. But then again you will only get an Integer as the result.

    5 / 2 = 2.5
    5 \ 2 = 2

    And since you can't ReDim your array to something like 12.32 anyway you should go with the \ operator.

    Best regards

  6. #6

    Thread Starter
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610
    Wow, you learn something new everyday.

    Thanks!
    This space for rent...

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