Solved

Byte Array

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Populate DataModel with DataTable 44 28
VB.Net WebRequest Code from JSON curl 9 13
.NET 2008 VB and C# 6 28
VB.net and sql server 4 36
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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