Solved

Byte Array

Posted on 2011-09-30
4
341 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 28
Problem with SqlConnection 5 117
Open a folder with partial name 14 29
Setting location of a form 4 22
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now