I’ve fallen victim to one of the classic blunders

No, its nothing concerning Sicilians or land wars in Asia, its worse!

Check out the following snippet of VB6 code

Dim lengthOfFile As Integer
lengthOfFile = LOF(fileHandle)

wholeFile = Input(lengthOfFile, #fileHandle)

Dim inStrPos As Integer
inStrPos = InStr(0, wholeFile, OldFileName, vbBinaryCompare)

If (inStrPos > 0) Then
‘ We have a match
End if

Nothing particularly wrong with that code, but I kept on getting “Run time error 5 : Invalid procedure call or argument”

After much more deliberation and cogitation that it deserved, I realised I had fallen victim to one of the classic Visual Basic blunders? Have you guessed what I’m about to reveal? Thats right, Visual Basic array functions start from 1 NOT 0… Gah! I’ve done too much C# lately, bah, easy to forget. Whats really galling, is arrays in VB start at 0, but can be rebased with an option switch, one thats not on by default. So why arrays at zero, but all functions for arrays at 1? bah.

The code line should read

Dim inStrPos As Integer
inStrPos = InStr(1, wholeFile, OldFileName, vbBinaryCompare)

Hopefully I’ll be done with this legacy codebase in the next 6 months, then it can be laid to rest for good.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s