Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

WTF, VB6? Part 4

This is the fourth post in the WTF, VB6? series
The previous ones can be found here:
Part 1
Part 2
Part 3

VB6 allows one to declare an array having any low subscript one wants, not only zero.
This is valid:

Dim MonthLengths(1 To 12) As Integer

And so is this:

Dim Grades(-20 To 20) As Single

As any array can have any low subscript, there is a native function to get it: LBound.
(The high subscript can be get using UBound function.)

Also, you can declare an empty array:

Dim Names() As String

If you wanted to know the above array's low subscript and would use LBound, you'd get... an error.
WTF, VB6?!?

This is an inconsistency in the language.
Over time, people found out ways to check whether an array was empty or not.
One way is to use the awkward expression below:

If (Not Names) Then

It works, except when doesn't. Sometimes it triggers an error that's completely unrelated (16: Expression too complex). So it is not very safe to use it.

Another way is to use the CopyMemory/RtlMoveMemory API and dig into VB6's array's implementation depths.
But there's another way, though: You can use the below function instead.

Public Function IsEmptyArray(ByVal Var As Variant) As Boolean
Dim Result As Boolean
Dim B() As Boolean
Dim I() As Integer
Dim L() As Long
Dim C() As Currency
Dim G() As Single
Dim D() As Double
Dim T() As Date
Dim S() As String
Dim V() As Variant
If Not IsArray(Var) Then Err.Raise 13

Select Case VarType(Var) Xor vbArray
Case vbBoolean
B() = Var
Result = Not B()

Case vbByte
Result = LenB(CStr(Var)) = 0

Case vbInteger
I() = Var
Result = Not I()

Case vbLong
L() = Var
Result = Not L()

Case vbCurrency
C() = Var
Result = Not C()

Case vbSingle
G() = Var
Result = Not G()

Case vbDouble
D() = Var
Result = Not D()

Case vbDate
T() = Var
Result = Not T()

Case vbString
S() = Var
Result = Not S()

Case vbObject, vbUserDefinedType
Result = LBound(Var) <= UBound(Var)

Case vbVariant
V() = Var
Result = Not V()
End Select

IsEmptyArray = Result
End Function

Andrej Biasic
2020-05-06