PDA

View Full Version : Subscript out of range error when trying to OR two arrays together.



Zuccj
07-07-2017, 05:13 AM
I have two arrays of the same size, and trying to bit-wise OR them into another array of the same size. When the program gets to the line of code that does the or I'm getting run time error 9: Subscript out of range.


Sub zap_chip()
Dim zap_mask(0 To 15) As Long
Dim verify_low(0 To 15) As Long
Dim verify_high(0 To 15) As Long
Dim error_mask(0 To 15) As Long
Dim zap_req As Boolean

ulMHL200Error = MHL200_GetRegister(ulMHL200Handle, &H0, verify_low(0))
.
.
.
Array getting populated, just showing one to save space

Dim k As Integer
For k = 0 To 15 Step 1
error_mask(k) = verify_low(k)
verify_high(k) = verify_low(k)
Next k

MsgBox "Testing Low Verification" & vbNewLine & "Press Any Key To Continue."

ulMHL200Error = MHL200_ZapVerify(ulMHL200Handle, 0, verify_low, error_mask)

If (ulMHL200Error = eMHL200_OK) Then
MsgBox "Low Verification Complete" & vbNewLine & "Press Any Key To Continue To High Verification"
ulMHL200Error = MHL200_ZapVerify(ulMHL200Handle, 1, verify_high, error_mask)
End If

Dim i As Integer
If (ulMHL200Error = eMHL200_ZAP_IMPOSSIBLE) Then
MsgBox "Zapping Impossible. An Already zapped bit can not be 0."
For i = 0 To 15 Step 1
If (error_mask(i) <> 0) Then
MsgBox "Error Mask Register 0x" & i & vbNewLine & ":0x" & error_mask(i)
End If
Next i
Exit Sub

ElseIf (ulMHL200Error <> eMHL200_OK) Then
MsgBox "An Error Occured During Verification." & vbNewLine & "Error Code:" & ulMHL200Error
Exit Sub

ElseIf (ulMHL200Error = eMHL200_OK) Then
MsgBox "Verification Complete" & vbNewLine & "Press Any Key to Continue Zapping"
End If

zap_req = False
For i = LBound(zap_mask) To UBound(zap_mask)
MsgBox "test 1"
zap_mask(i) = verify_low(i) Or verify_high(i)
If (zap_mask(i) <> 0) Then
MsgBox "test 2"
zap_req = True
End If
Next i

If (zap_req = True) Then
ulMHL200Error = MHL200_ZapEx(ulMHL200Handle, zap_mask)
If (ulMHL200Error = eMHL200_OK) Then
MsgBox "Zapping Complete"
Else: MsgBox "An Error occured during Zapping"
End If
Else: MsgBox "All Bits Are Zapped"
End If
End Sub

The line in red is what is giving me the error. Test 1 is displayed only once, so the programing is failing the second it hits that line. Not sure what can be causing it, as from what I saw online it mostly means something is either out of range, or named wrong which I don't see in this case. The section in the middle is just error checking, and should have no affect on the arrays.

Edit: just realized the line didn't get highlighted that is throwing the error.

>>>zap_mask(i) = verify_low(i) Or verify_high(i)

That is the line that is throwing the subscript error

mdmackillop
07-07-2017, 05:36 AM
Try

Dim zap_mask(0 To 15) As Variant

It looks like zap_mask(i) = verify_low(i) Or verify_high(i) will return Boolean

mdmackillop
07-07-2017, 05:43 AM
Second thoughts
verify_low(i) and verify_high(i) are both Long
Is your code in effect saying zap_mask(i) = 5 or 10

Sub Test()
Dim k
k = 5 Or 10
MsgBox k
End Sub

Zuccj
07-07-2017, 05:46 AM
That could be what is causing it. I'm still new to vba is there a way to or two numbers together and not get a boolean result? For each element of verify low and high it can be a number anywhere from 0 to 255. I want zap_mask to be 0-255 as well, but just a bitwise or between the two values.

Zuccj
07-07-2017, 05:54 AM
The reason they are long is the dll I'm using is expecting a long in the function calls.

mdmackillop
07-07-2017, 06:07 AM
OR is adding the values. See this simplified test code

Sub zap_chip()
Dim zap_mask(0 To 15) As Long
Dim verify_low(0 To 15) As Long
Dim verify_high(0 To 15) As Long
Dim error_mask(0 To 15) As Long
Dim zap_req As Boolean

Dim k As Integer
For k = 0 To 15 Step 1
verify_low(k) = Int(100 * Rnd())
verify_high(k) = Int(100 * Rnd() + 100)
Next k

zap_req = True
For i = LBound(zap_mask) To UBound(zap_mask)
zap_mask(i) = verify_low(i) Or verify_high(i) 'ERROR
If (zap_mask(i) <> 0) Then
msg = msg & verify_low(i) & "; " & verify_high(i) & "; " & zap_mask(i) & vbCr
End If
Next i
MsgBox msg

End Sub

Zuccj
07-07-2017, 06:17 AM
It is doing an or operation for me when I run it. Using that test code:

first number 10 in binary: 00001010
second number 179 in binary: 10110011
result 187 in binary: 10111011

10111011 is the or of 00001010 and 10110011.

Edit: that code did compile and run though, so I guess I'll start commenting out lines and see if why it is throwing an error in my code.

mdmackillop
07-07-2017, 06:42 AM
I've not used Binary in VBA. If I convert my simple test to Binary inputs, OR is not giving a Binary result according to the msg line but I'm probably missing something.

Paul_Hossler
07-07-2017, 07:45 AM
Q: You have verify_low ( 0..15 ) As Long. Does that represent 16 32-bit Longs, or are you only interested in the low order 16 bits of a single 32-bit Long?

Q: Since we don't have your DLL, can you make a test WB with representative data that does show the issue but delete lines that don't cause a problem?

Maybe add a testing line like



verify_low = Array(12,14,16,19......, 15)


Q: You said it have to be a Long for DLL calling, but are you really only interested in byte values (0..255)?