[RESOLVED] [2008] Array.IndexOf always returns -1
I am having trouble with the following piece of code, the searchIndex always returns -1. If I put a breakpoint after 'searchIndex = Array.IndexOf(buffer, &H90)' and look in the buffer array I can see 90h so it should return the index but it doesn't.
This code is in my serialport DataReceived sub
Any help would be great!
Thanks
Code:
Dim searchIndex As Integer
Dim buffer() As Byte = New Byte() {}
Dim bytesRead As Integer
bytesRead = SerialPort.BytesToRead ' Get how many bytes available.
Array.Resize(buffer, bytesRead) ' Set the buffer to the number of bytes available.
bytesRead = SerialPort.Read(buffer, 0, bytesRead) ' Read the bytes
If bytesRead > 0 Then
searchIndex = Array.IndexOf(buffer, &H90)
If searchIndex <> -1 AndAlso buffer(searchIndex + 1) = &H90 Then
If (searchIndex + 18) <= buffer.Length Then
For x As Integer = searchIndex To (searchIndex + 18)
Debug.WriteLine(buffer(x).ToString)
Next
End If
End If
End If
Re: [2008] Array.IndexOf always returns -1
Code:
Dim buffer() As Byte = New Byte() {1, 2, 10, 11}
Dim lk4 As Byte = &HA
Dim iof As Integer = Array.IndexOf(buffer, lk4)
your code looks like something i have seen b4.
Re: [2008] Array.IndexOf always returns -1
Yes I think you may have :D
searchIndex now returns correctly, thanks
Re: [2008] Array.IndexOf always returns -1
remember to mark thread resolved, etc.