locked
Byte Array LINQ RRS feed

  • Question

  • I have a byte array which I want to check whether it contains a value

    If m_bytInput(iPos - 1) = CRFIDSyrisPacket.RFIDSyrisLineEnd

    I tried but it never evaluates to true even when there is 

               'm_bytInput.Any(Function(X) X =CRFIDSyrisPacket.RFIDSyrisLineEnd )
    
                        

    Thursday, October 6, 2016 11:08 AM

Answers

  •  Why not just use the Contains method of the array?

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bts() As Byte = {12, 0, 106, 25}
            If bts.Contains(106) Then
                MessageBox.Show("Contains 106")
            End If
        End Sub
     

     

     However,  if testing against an Enum,  you will need to convert the Enum member to a Byte to compare it.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bts() As Byte = {12, 0, 106, 25}
            If bts.Contains(CByte(SomeEnum.B)) Then
                MessageBox.Show("Contains 106")
            End If
        End Sub
    
    End Class
    
    Public Enum SomeEnum
        A = 10
        B = 106
        C = 228
    End Enum




    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, October 6, 2016 6:29 PM
    • Proposed as answer by Neda Zhang Friday, October 7, 2016 2:49 AM
    • Marked as answer by Neda Zhang Tuesday, October 18, 2016 1:41 AM
    Thursday, October 6, 2016 3:20 PM

All replies

  • Not much to go on here.  If m_bytInput is an array, and CRFIDSyrisPacket.RFIDSyrisLineEnd is A byte, then the Any method will return true.  As a test I wrote this to prove that it works.  As you will notice I tried several different byte values.

            Dim theChars As String = " !#$%&""'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
            'generate some bytes to TEST with
            Dim testBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(theChars) 'convert chars to bytes
            'get byte to look for
            Dim look4This As Byte = testBytes(theChars.IndexOf("+"c))
            'Dim look4This As Byte = testBytes(theChars.IndexOf("±"c))
            'Dim look4This As Byte = 0 'not in the array
    
            Dim found As Boolean = testBytes.Any(Function(b) b = look4This)
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, October 6, 2016 11:35 AM
  • The array is a byte array, I am comparing against an int value.  The problem is it does not pick up when there is a 

    RFIDSyrisLineEnd 

    Thursday, October 6, 2016 12:05 PM
  •  Why not just use the Contains method of the array?

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bts() As Byte = {12, 0, 106, 25}
            If bts.Contains(106) Then
                MessageBox.Show("Contains 106")
            End If
        End Sub
     

     

     However,  if testing against an Enum,  you will need to convert the Enum member to a Byte to compare it.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bts() As Byte = {12, 0, 106, 25}
            If bts.Contains(CByte(SomeEnum.B)) Then
                MessageBox.Show("Contains 106")
            End If
        End Sub
    
    End Class
    
    Public Enum SomeEnum
        A = 10
        B = 106
        C = 228
    End Enum




    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, October 6, 2016 6:29 PM
    • Proposed as answer by Neda Zhang Friday, October 7, 2016 2:49 AM
    • Marked as answer by Neda Zhang Tuesday, October 18, 2016 1:41 AM
    Thursday, October 6, 2016 3:20 PM
  • The array is a byte array, I am comparing against an int value.  The problem is it does not pick up when there is a 

    RFIDSyrisLineEnd 


    What is the value stored in the variable RFIDSyrisLineEnd? I am guessing it is > 255.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Friday, October 7, 2016 11:37 AM