How Can I Test a Dynamic Array to See if the Array is Empty?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I test a dynamic array to see if the array is empty?

— SH

SpacerHey, Scripting Guy! AnswerTechNet Script Center

Hey, SH. Today’s column is dated Wednesday, July 5th. If there really is a column today that means that, unlike last year, the neighbors and their errant fireworks didn’t set fire to the Scripting House. If there isn’t a new column today, well, to paraphrase Britney Spears: oops, they did it again.

Note. In case you’re wondering, if the neighbors did set the house on fire two years in a row, this Scripting Guy probably wouldn’t say “oops.” Pardon? Well, yes, he probably would say that. And that. And – well, no not that. Not even if someone burned down his house!

As you might expect, last year’s Fourth of July was a bit more eventful than this year’s; things tend to be more memorable when they involve fires roaring through your garage and bedrooms filling up with smoke. It was a bit disconcerting, too; after all, why would anyone want to burn this Scripting Guy’s house down?

Wait, never mind: turns out that we can think of lots of reasons why someone might want to burn his house down. In fact, one or more of his fellow Scripting Guys is likely to bring that up as a topic for discussion at each and every team meeting. In fact, come to think of, SH, that idea might even begin to appeal to you if we don’t quit rambling on and answer your question.

Trust us, one house fire is enough for anyone. With that in mind, here’s a script that can tell you whether or not a dynamic array is empty (that is, whether or not the array has any items in it):

On Error Resume Next

Dim arrTest() ReDim Preserve arrTest(0)

intUpper = Ubound(arrTest)

If Err = 0 Then Wscript.Echo “This array is not empty.” Else Wscript.Echo “This array is empty.” Err.Clear End If

Dim arrTest2()

intUpper2 = Ubound(arrTest2)

If Err = 0 Then Wscript.Echo “This array is not empty.” Else Wscript.Echo “This array is empty.” Err.Clear End If

Before we begin, we should probably note that there might very well be some other, really cool way of determining whether an array has any items in it or not; however, we didn’t know what that might be. Therefore, we came up with this solution: all we do is try and retrieve the upper bound (i.e., the index number of the last item) in the array. If the call to the Ubound function succeeds, then the array must have at least one item in it. If the call fails, then the array must be empty. It’s not the most sophisticated script ever written, but it works.

Speaking of the script, it starts off with the On Error Resume Next statement. This is important. As we noted, we’re going to check the upper bound of the array in question. If the array is empty, the Ubound function triggers an error; in turn, that will cause the script to come to a screeching halt. That is, it will come to a screeching halt unless we’ve implemented On Error Resume Next.

Did we mention that that was important? It is.

That brings us to these two lines of code:

Dim arrTest()
ReDim Preserve arrTest(0)

All we’re doing here is creating a dynamic array named arrTest; that’s what happens in line 1. In line 2, we then use the ReDim statement to resize arrTest, giving it a size of 1. (Remember, the number of items in an array is always one more than the subscript passed to the Dim or ReDim statements. Thus a command like ReDim Preserve arrTest(0) gives us an array of one item [0 + 1]).

Once we have our one-item array we then call the Ubound function, storing the upper bound in a variable named intUpper:

intUpper = Ubound(arrTest)

We don’t really care what the upper bound is, we’re just interested in knowing if the array has an upper bound. In this case, it should: because arrTest isn’t empty, the Ubound function should work just fine and no error should occur. We can verify that by checking the value of the Err object using this block of code:

If Err <> 0 Then
    Wscript.Echo “This array is empty.”
    Err.Clear
Else
    Wscript.Echo “This array is not empty.”
End If

If Err equals 0, we echo back the fact that the array is not empty; remember, had the array been empty an error would have occurred and Err would be equal to something other than 0. (Any time Err equals 0 that means no error has occurred.) If Err is equal to something other than 0, that must mean the array is empty. Accordingly, we echo back that fact, then use the Err.Clear method to reset the Err object. That’s all we have to do; just like that we’ve determined whether or not the array is empty.

Note. What’s that? You don’t understand why we need to reset the Err object? For the answer, take a few minutes out from your busy schedule and take a look at Hey, Scripting Guy! The Movie.

Of course, there is a second part to the script, but that section is primarily a repeat of part 1. There is, however, one important exception: this time around we create a dynamic array name arrTest2, but we never add anything to that array. It’s an empty array, and, with any luck, the script should tell us that.

Hope that helps, SH. As for us, we need to run home for a minute and make sure everything is OK. After all, the next Fourth of July is now less than a year away, and we’re still a little gun-shy.

Note. OK, so technically we’re not really worried about the house; we just felt like leaving work and going home. But no one has to know that, right?

0 comments

Discussion is closed.

Feedback usabilla icon