when does vba ubound function return minus one (-1)

I sometimes define dynamic arrays as follows

dim ary()
redim ary(-1 to -1)  ' useful for "isempty" tests:  if ubound(ary) = -1 then debug.print "it is empty"

But, I seem to remember that there are other conditions in which ubound can return minus one.  

This is a vba questions, not a visual studio question. (visual studio help file describes a minus 1 return, but that is not helpful).

Who is Participating?
Rory ArchibaldCommented:
If you declare an array like:
Dim ary(-2 to -1)

Open in new window

then Ubound would of course return -1.

It will also be the case if say you use a function like Filter (that always returns an array) and nothing in the array matches your criteria - in which case UBound returns -1 and LBound returns 0.
Martin LissOlder than dirtCommented:
I don't know of any other situation where uBound would give -1. If you just do

Dim ary()
MsgBox uBound(ary)

you'd get an error.
rberkeConsultantAuthor Commented:
Aha !!!

In the distant past I had code that used

x = split(strParameters)
if ubound(x) = -1 then doSomething

But, my memory played a trick on me, and all I could remember was that ubound sometimes returned -1.  It was really bugging me that I could not remember WHY. With Rory's help I now remember that it happened when strParameters contained an empty string.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.