Testing if an array has been initialized in VB6

In VB6 it is difficult to test if an array has been initialized without raising an error because any attempt to reference an uninitialized array (say to perform a UBound() or LBound() check raises runtime error 9 'Subscript out of range.' IsEmpty and IsNull also do not work.

The following convoluted bit of logic will allow you to test if an array has been initialized without raising an error:

If (Not (Not varArray) = True) Then
   ' the array is initialized
Else
   ' the array is not initialized
End If

The order of operations is extremely important here (and frankly, I'm not quite sure why). I attempted to simplify the statement to ((Not varArray) = False) and it returned an incorrect result. I've stuck with this because I know it works.

I originally found this on a newsgroup a long time ago... but can't find the original post to save me. If you know the original source, I'll be happy to post it.

Another common solution is using a separate function to trap the runtime error. An example of this can be found here.


Author: ASAK
Created: May 17 2007
Categories: Visual Basic
TechByte #151

Warning: By visiting this site and/or by using any information contained herein, you agree to the Techbytes.ca terms of use.



Add a comment about this TechByte

If you wish to add a comment regarding this TechByte, please use the form below. Please note that by submitting comments using this form you are allowing all of the information submitted to be visible on this website. Any comments submitted using this form will only be shown on the website if they are approved by the administrators of this site. IF APPROVED, COMMENTS MAY TAKE SEVERAL DAYS TO BE POSTED.

Posted By: (Optional)

Comments:


Other TechBytes: