Results 1 to 6 of 6

Thread: VB6 Subscript out of range - ARRAY

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    VB6 Subscript out of range - ARRAY

    I call a third party function that returns an array. I dimension the array to zero elements before calling the function. However, occasionally the function returns an array with no upper or lower bound (similar to creating a modular level dynamic array). How can I check for this situation without using an error handling routine? I currently check the array's UBound, but on this occurrence a 'subscript out of range' occurs. Code snippet shown below:

    Code:
        'Reset the sensor id byte array.
        ReDim bytSensorID(0) As Byte
        
        'Determine if the device has been initialized.
        If mboolInitialized Then
            'Device has been initialized.
            '   Request the sensor id information.
            bytSensorID = mobjSATTester.getSensorIDByteArray()
            'This statement errors on occasion with subscript out of range
            lngUpperBound = ubound(bytSensorID)

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB6 Subscript out of range - ARRAY

    How about If UBound(bytSensorID) > 0 Then

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: VB6 Subscript out of range - ARRAY

    Try the code below. It will generate a 'subscript out of range' error. The array hasn't been dimensioned. How can I check if an array hasn't been dimensioned?

    Code:
    Option Explicit
    Private mstrArray() As String
    
    Private Sub Command1_Click()
    
        If UBound(mstrArray) > 0 Then
            MsgBox "here"
        End If
        
    End Sub

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: VB6 Subscript out of range - ARRAY

    Use this:
    Code:
    ' Returns 0 for unintialized array, -1 for non-array
    Public Function ArrayDimensions(pvarArray As Variant) As Long
        Dim lngTemp As Long
        Dim i As Long
        
        On Error Resume Next
        Do
            i = i + 1
            lngTemp = UBound(pvarArray, i)
            Select Case Err.Number
                Case 13: ArrayDimensions = -1
                Case 9: ArrayDimensions = i - 1
            End Select
        Loop Until Err.Number
    End Function

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: VB6 Subscript out of range - ARRAY

    Yep. I like it. Didn't know if it was possible without using vb error handling. I've tried IsArray, IsNull, IsEmpty. Seems weird that there isn't a canned function for this in VB6.

    Quote Originally Posted by Ellis Dee
    Use this:
    Code:
    ' Returns 0 for unintialized array, -1 for non-array
    Public Function ArrayDimensions(pvarArray As Variant) As Long
        Dim lngTemp As Long
        Dim i As Long
        
        On Error Resume Next
        Do
            i = i + 1
            lngTemp = UBound(pvarArray, i)
            Select Case Err.Number
                Case 13: ArrayDimensions = -1
                Case 9: ArrayDimensions = i - 1
            End Select
        Loop Until Err.Number
    End Function

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: VB6 Subscript out of range - ARRAY

    If you really want to avoid error handling, see this thread. As much as I hate intentionally raising errors, this is one instance where IMO it is preferable.

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