Page 2 of 2 FirstFirst 12
Results 41 to 44 of 44

Thread: ubound; subscript out of range

  1. #41

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206
    rawr my mistake!

  2. #42
    New Member
    Join Date
    Nov 2010
    Posts
    4

    Re: ubound; subscript out of range

    Quote Originally Posted by Merri View Post
    VB Code:
    1. If (Not ArrayName) = True Then
    2.     'the array is empty, you get an error if you use UBound or LBound
    3. Else
    4.     'the array has items, you can use LBound and UBound
    5. 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.

  3. #43
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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
    W o t . S i g

  4. #44
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width