Solved

Byte Array

Posted on 2011-09-30
4
353 Views
Last Modified: 2012-06-22
Wheat is the best way to check a byte array to see if it has changed, I have the following property

    Public Property MergeDocument() As Byte()
        Get
            Return _mergeDocument
        End Get
        Set(ByVal value As Byte())
            If _mergeDocument ??? value Then
                If Not Loading Then
                    _isDirty = True
                End If
                _mergeDocument = value
            End If
        End Set
    End Property

I also need to do a check to see if it is empty, is it best to just use .Count?
0
Comment
Question by:rocky050371
  • 2
4 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36891483
If both arrays are already in memory, you can just use a compare function:

http://stackoverflow.com/questions/627742/what-is-the-fastest-way-to-compare-two-byte-arrays/627767#627767
Public Shared Function ArraysEqual(ByVal first As Byte(), _
                                   ByVal second As Byte()) As Boolean
    If (first Is second) Then
        Return True
    End If

    If (first Is Nothing OrElse second Is Nothing) Then
        Return False
    End If
    If  (first.Length <> second.Length) Then
         Return False
    End If

    For i as Integer = 0 To first.Length - 1
        If (first(i) <> second(i)) Then
            Return False
        End If
    Next i
    Return True
End Function

Open in new window


If you have to read one of them from storage, it would be better to use a stream and only read in enough of it/them to prove inequality.
0
 

Author Comment

by:rocky050371
ID: 36891544
 If (first Is Nothing OrElse second Is Nothing) Then
        Return False
    End If

Surely if they are both empty then an incorrect value would be returned

Is the following based on them both pointing to the same memory address

  If (first Is second) Then
        Return True
    End If

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36891581
0
 
LVL 18

Expert Comment

by:lludden
ID: 36891589
You can add in
If First Is Nothing AND Second Is Nothing) Then
    Return True
End if

(do it before the comparison you listed)

Yes.  If you are comparing an object to itself, it will always be equal.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question