I know you're looking for something else, but I would use On Error. You don't have to just resume next if you feel that it is an inelegant solution.
You can On Error Resume Next and then access a property of a control that may or not be in the array, and then check to see if err.number<>0 - that would tell you that the item does not exist. You could then clear the error & go on to the next one. If err.number=0 then the control exists & you can perform your operation.
Obviously if you're only performing one operation & don't need to track whether the control exists or not, it would be simpler just to protect from errors and run through them all without regard to whether or not the operation did anything.
try to create the object.. if it gives you an error then an object exists in that location.
you would use "On Error Resume Next" and "On Error Goto 0".
Code:
On Error Resume Next
Load ArrayCtl(23)
If (Err.Number <> 0) Then
'Clear the error
Err.Clear
Resume
'Do what ever you would want if the object did not exist
...
...
Else
'Do what ever you would want if the object did exist
...
...
End If
On Error Goto 0
You can also simply access the object instead of attempting to create it. The same approach can be used for both.
I even tried ObjPtr(), and was surprised to see it returning a value for non-existant control array elements.
I tried that too! And VarPtr(). It seems the array is redimensioned and the objects instantiated, but they don't go into the proper "loaded" state. Very strange.
Using Vartype(Check1(6)) <> 9 seems to work without the need of error handling. When the array element exists it returns apparently the type of the default property of the control while when it does not exist returns 9 (vbObject) without raising a error. But yet very weird.
Using VarType will work IF the control has a default member and that default member returns value other than vbObject. If the control doesn't have a default member, it will always return false.
One solution is to enumerate the items in the control array and compare the Index of each item.
Code:
Function CheckCtrlIndex(ctrlArray As Object, ByVal idx As Integer) As Boolean
Dim result As Boolean, ctrl As Control
For Each ctrl In ctrlArray
If ctrl.Index = idx Then
result = True
Exit For
End If
Next ctrl
CheckCtrlIndex = result
End Function
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.