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)
Re: VB6 Subscript out of range - ARRAY
How about If UBound(bytSensorID) > 0 Then
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
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
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
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.