Solved

Byte Array

Posted on 2011-09-30
4
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

628 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