{"id":66973,"date":"2006-07-05T17:13:00","date_gmt":"2006-07-05T17:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/05\/how-can-i-test-a-dynamic-array-to-see-if-the-array-is-empty\/"},"modified":"2006-07-05T17:13:00","modified_gmt":"2006-07-05T17:13:00","slug":"how-can-i-test-a-dynamic-array-to-see-if-the-array-is-empty","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-test-a-dynamic-array-to-see-if-the-array-is-empty\/","title":{"rendered":"How Can I Test a Dynamic Array to See if the Array is Empty?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I test a dynamic array to see if the array is empty?<BR><BR>&#8212; SH<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"TechNet Script Center\" height=\"288\" alt=\"TechNet Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, SH. Today\u2019s column is dated Wednesday, July 5<SUP>th<\/SUP>. If there really <I>is<\/I> a column today that means that, unlike last year, the neighbors and their errant fireworks didn\u2019t set fire to the Scripting House. If there <I>isn\u2019t<\/I> a new column today, well, to paraphrase Britney Spears: oops, they did it again. <\/P>\n<TABLE class=\"dataTable\" id=\"ECD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019re wondering, if the neighbors <I>did<\/I> set the house on fire two years in a row, this Scripting Guy probably wouldn\u2019t say \u201coops.\u201d Pardon? Well, yes, he probably <I>would<\/I> say that. And that. And &#8211; well, no not that. Not even if someone burned down his house!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you might expect, last year\u2019s Fourth of July was a bit more eventful than this year\u2019s; 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\u2019s house down? <\/P>\n<P>Wait, never mind: turns out that we can think of <I>lots<\/I> 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\u2019t quit rambling on and answer your question.<\/P>\n<P>Trust us, one house fire is enough for anyone. With that in mind, here\u2019s 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):<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Dim arrTest()\nReDim Preserve arrTest(0)<\/p>\n<p>intUpper = Ubound(arrTest)<\/p>\n<p>If Err = 0 Then\n    Wscript.Echo &#8220;This array is not empty.&#8221;\nElse\n    Wscript.Echo &#8220;This array is empty.&#8221;\n    Err.Clear\nEnd If<\/p>\n<p>Dim arrTest2()<\/p>\n<p>intUpper2 = Ubound(arrTest2)<\/p>\n<p>If Err = 0 Then\n    Wscript.Echo &#8220;This array is not empty.&#8221;\nElse\n    Wscript.Echo &#8220;This array is empty.&#8221;\n    Err.Clear\nEnd If\n<\/PRE>\n<P>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\u2019t 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 <B>Ubound<\/B> function succeeds, then the array must have at least one item in it. If the call fails, then the array must be empty. It\u2019s not the most sophisticated script ever written, but it works.<\/P>\n<P>Speaking of the script, it starts off with the <B>On Error Resume Next<\/B> statement. This is important. As we noted, we\u2019re going to check the upper bound of the array in question. If the array is empty, the <B>Ubound<\/B> 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\u2019ve implemented On Error Resume Next.<\/P>\n<P>Did we mention that that was important? It is.<\/P>\n<P>That brings us to these two lines of code:<\/P><PRE class=\"codeSample\">Dim arrTest()\nReDim Preserve arrTest(0)\n<\/PRE>\n<P>All we\u2019re doing here is creating a dynamic array named arrTest; that\u2019s what happens in line 1. In line 2, we then use the <B>ReDim<\/B> 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 <B>ReDim Preserve arrTest(0)<\/B> gives us an array of one item [0 + 1]). <\/P>\n<P>Once we have our one-item array we then call the Ubound function, storing the upper bound in a variable named intUpper:<\/P><PRE class=\"codeSample\">intUpper = Ubound(arrTest)\n<\/PRE>\n<P>We don\u2019t really care what the upper bound is, we\u2019re just interested in knowing if the array <I>has<\/I> an upper bound. In this case, it should: because arrTest isn\u2019t empty, the Ubound function should work just fine and no error should occur. We can verify that by checking the value of the <B>Err<\/B> object using this block of code:<\/P><PRE class=\"codeSample\">If Err &lt;&gt; 0 Then\n    Wscript.Echo &#8220;This array is empty.&#8221;\n    Err.Clear\nElse\n    Wscript.Echo &#8220;This array is not empty.&#8221;\nEnd If\n<\/PRE>\n<P>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 <B>Err.Clear<\/B> method to reset the Err object. That\u2019s all we have to do; just like that we\u2019ve determined whether or not the array is empty.<\/P>\n<TABLE class=\"dataTable\" id=\"EDF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. What\u2019s that? You don\u2019t 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 <A href=\"mms:\/\/wm.microsoft.com\/ms\/technet\/scripting\/hey_scripting_guy_the_movie.wmv\" target=\"_blank\"><B>Hey, Scripting Guy! The Movie<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, there <I>is<\/I> 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\u2019s an empty array, and, with any luck, the script should tell us that.<\/P>\n<P>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\u2019re still a little gun-shy.<\/P>\n<TABLE class=\"dataTable\" id=\"E1F\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. OK, so <I>technically<\/I> we\u2019re not really worried about the house; we just felt like leaving work and going home. But no one has to know that, right?<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I test a dynamic array to see if the array is empty?&#8212; SH Hey, SH. Today\u2019s 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\u2019t set fire to the Scripting House. If there [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[18,3,4,5],"class_list":["post-66973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-arrays-hash-tables-and-dictionary-objects","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I test a dynamic array to see if the array is empty?&#8212; SH Hey, SH. Today\u2019s 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\u2019t set fire to the Scripting House. If there [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66973\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}