|
-
Aug 28th, 2000, 11:26 AM
#1
Thread Starter
Fanatic Member
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
-
Aug 28th, 2000, 12:06 PM
#2
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.
-
Aug 28th, 2000, 12:11 PM
#3
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
-
Aug 28th, 2000, 12:31 PM
#4
Thread Starter
Fanatic Member
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.
-
Aug 28th, 2000, 12:35 PM
#5
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
-
Aug 28th, 2000, 12:46 PM
#6
Thread Starter
Fanatic Member
Wow, you learn something new everyday.
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|