I recently got this question from a user working with arrays.
How do I easily assign an array to null? It's a string array, and
I've tried: Array = Null, Array = Empty, Array = "" and
I always get a Type Mismatch error.
I suppose I could build a custom function to cycle through the array
and assign each part to "", but is there a VBScript function
that already does this?
Well, while there isn't a built in VBScript array emptying
mechanism, if you ReDim the array to the samne size without
using the PRESERVE keyword, it achieves the desired result.
So, assuming arrTest is your array, this code:
ReDim arrTest(UBound(arrTest))
will get the job done and you don't even have to loop through
the array to do it.
Note: That just goes to show what I know... there actually is a VBScript array emptying mechanism! It's the
Erase
statement. It works with fixed arrays as well (which the ReDim statement doesn’t). So now you've got a choice of how to empty your arrays. Thanks to Zack Jones for giving me the heads up.
If you have a tip you would like to submit, please send it to:
webmaster@asp101.com.