r/visualbasic • VB.Net Intermediate • Nov 21 '24

VB6 Help Other VB6/VBA/VBScript gotchas?

I notices that, VB6/VBA/VBScript have a gotcha in its language design; where subsequent conditions of an if statement, are evaluated even though they're not supposed to.

For array e.g.:

arr = array(3, 4, 5)
i = ubound(arr) + 5 'beyond array length
if (i < ubound(arr)) and isempty(arr(i)) then
  rem above line causes exception
end if

In above code, arr(i) is not supposed to be evaluated. But it does anyway.

Same thing goes to collection. e.g.:

set fl = createObject("scripting.filesystemobject").getfolder(".").files
i = fl.count + 5 'beyond collection length
if (i < fl.count) and isempty(fl(i)) then
  rem above line causes exception
end if

Or object. e.g.:

set x = nothing
if (not (x is nothing)) and isempty(x.prop) then
  rem above line causes exception
end if

I already know the workaround for above gotcha, and I'm not asking for other workaround, or any alternative language.

I want to know any other kind of gotcha in VB6/VBA/VBScript.

4 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/Hel_OWeen Nov 22 '24

It's not a bug as it is explained in TFM:

' Multiple declarations on a single line. AnotherVar is of type Variant
' because its type is omitted.
Dim AnotherVar, Choice As Boolean, BirthDate As Date

I know, why bother reading manuals these days if you can ask SO/Reddit instead and let others do the work for you?

1

u/BCProgramming Nov 22 '24

Not only that, but it's pretty much how structured BASIC languages have worked since the 80's; Of course back then the default data type was Single, since Variant wasn't introduced until VB 2.0). The idea that it is a bug is silly simply because it is like saying there's a bug in Pascal where braces cause code to be ignored, (because that is how you enter comments).