|
-
Oct 11th, 2004, 02:44 PM
#41
Thread Starter
Banned
-
Nov 19th, 2010, 11:53 AM
#42
New Member
Re: ubound; subscript out of range
 Originally Posted by Merri
VB Code:
If (Not ArrayName) = True Then
'the array is empty, you get an error if you use UBound or LBound
Else
'the array has items, you can use LBound and UBound
End If

This does not work, it apparently never reaches Else when ArrayName is populated. Unfortunately, found out when a project manager noticed something wasn't working anymore. Here is the code I had to use to get the check working properly:
Code:
Function EmptyStrArray(ByRef StrArray() As String) As Boolean
Dim i As Integer
On Error GoTo Return_Empty
i = UBound(StrArray)
EmptyStrArray = False
Exit Function
Return_Empty:
EmptyStrArray = True
End Function
You can replace String data type with variant or whatever data type you are using.
-
Nov 19th, 2010, 12:26 PM
#43
Re: ubound; subscript out of range
I see its your first post here, welcome to the forum.
Unfortunately you have got off to a bad start, firstly this thread is 6 years old, secondly that code does actually work (but it comes with a funny caveat)
That said welcome again and I'm glad you have fixed your bug
-
Nov 20th, 2010, 06:03 AM
#44
Re: ubound; subscript out of range
The fixed versions, which work for any type of array. Debug.Assert fixes buggy VB6 IDE behavior.
Version without new function:
Code:
Dim blnInit As Boolean
blnInit = Not Not Array
Debug.Assert App.hInstance
If blnInit Then
' array is initialized
Else
' array is not initialized, "empty"
End If
Version as a function:
Code:
Public Function ArrayInit(ByVal NotNotArray As Long) As Boolean
Debug.Assert App.hInstance
ArrayInit = NotNotArray
End If
Usage: If ArrayInit(Not Not YourArrayVariableHere) Then
Background:
Not Not Array returns the pointer to safe array header, ie. the 32-bit Long value that is held by the array variable. If array is not initialized and thus does not have a safe array header, then the value is 0. In any other case the array has been initialized and it does have a safe array header.
Safe array is the structure internally used by VB6 for arrays.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|