-1

I'm trying to translate a comparison between two Nullable(Of Byte) objects:

    public byte?[] header { get; private set; }

    public override bool Equals(object other)
    {
        // ... more code to translate
        if (this.header != otherType.header) return false;
        // ... more code to translate
    }

An online code translator gives me this equivalent:

    Private m_header As Nullable(Of Byte)()
    Public Property header() As Nullable(Of Byte)()
        Get
            Return m_header
        End Get
        Private Set(value As Nullable(Of Byte)())
            m_header = value
        End Set
    End Property

    Public Overrides Function Equals(other As Object) As Boolean

        ' ... more code translated
        If Me.header <> otherType.header Then
            Return False
        End If
        ' ... more code translated

    End Function

But I get this exception:

Operator '<>' is not defined for types '1-dimensional array of Byte?' and '1-dimensional array of Byte?'. Use 'Is' operator to compare two reference types.

Then, as the detail of the exception says, I would like to know if this should be the proper translation 'cause I'm not totally sure:

If Not Me.header Is otherType.header Then
    Return False
End If
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • When you are comparing 2 `header`s, what do you consider "equal"? If they are the exact same instance, or if they contain exactly the same values? – RB. Mar 06 '14 at 13:52
  • You could always use `If Not ReferenceEquals(Me.header, otherType.header) Then`, but a more knowledgeable VB.NET programmer will undoubtedly come up with a better answer. – Lasse V. Karlsen Mar 06 '14 at 13:53
  • @RB. C# does not do value comparison here, it will simply check for reference equality. – Lasse V. Karlsen Mar 06 '14 at 13:53
  • @LasseV.Karlsen I know that - I'm wondering if the author knows that! – RB. Mar 06 '14 at 13:54
  • `Use 'Is' operator to compare two reference types.` – string.Empty Mar 06 '14 at 13:54
  • @RB NO idea what to respond to your question 'cause I'm just trying convert a third party code as is : http://filetypedetective.codeplex.com/releases/view/80180 – ElektroStudios Mar 06 '14 at 13:54
  • 2
    If you're trying to blindly convert code without knowing the idioms of both language, you're going to have **heaps** of problems. Why do you need to convert? Why not just reference the assemblies? Do you need to change/maintain the code? – Lasse V. Karlsen Mar 06 '14 at 13:55
  • @Lasse V. Karlsen because I would like to extend the functionality of the source (adding more methods and byte marks), and for that I need to work it in VB.NET, but, I know, I have not the sufficient knowledges to touch all the parts of the source. – ElektroStudios Mar 06 '14 at 13:56
  • Got to ask. Why do you need it in VB.NET to extend the functionality? – paparazzo Mar 06 '14 at 14:07
  • @Blam 'cause I know better VB.NET syntax than C#, I'm not a C# developer. just I'm trying to convert the code to my working environment where I could work it better. I think that is what a good programmer should do! :P, thanks for comment – ElektroStudios Mar 06 '14 at 14:07
  • Only a comment and I don't mean to get on your case but if you don't know C# then you don't know if you have converted properly. To do this right you need to learn C#. – paparazzo Mar 06 '14 at 15:09

3 Answers3

3

You could use the null-coalescing operator to treat a null-byte as 0:

Public Overrides Function Equals(other As Object) As Boolean
    ' ... '
    Dim otherType = TryCast(other, ActualType)
    If otherType Is Nothing Then Return False
    If If(Me.header, 0) <> If(otherType.header, 0) Then
        Return False
    End If
    ' ... '
End Function

Here is an approach which checks if both nullable-arrays are equal:

If header Is Nothing AndAlso otherType.header Is Nothing Then
    Return True
ElseIf Object.ReferenceEquals(header, otherType.header) Then
    Return True
ElseIf header Is Nothing OrElse otherType.header Is Nothing Then
    Return False
Else
    Return header.SequenceEqual(otherType.header)
End If
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thankyou, but please, could you clarify if your solution is basically the same as Jon and Rondles answers? if not, your solution has advantages?, sorry but due to my shortage knowledges of working with Bytes I can't notice if this solution has beneffits than the other answers. – ElektroStudios Mar 06 '14 at 14:03
  • 1
    @ElektroStudios: Jon checks if both objects are the same reference, i'm checking if both objects are equal. My approach has one problem, it treats a byte that is null as 0, so `0` would be the same as `Nothing`. I'll edit my answer to show a better approach. – Tim Schmelter Mar 06 '14 at 14:06
  • 1
    @ElektroStudios: i have edited my answer. I haven't first noticed that it is a nullable byte-array. – Tim Schmelter Mar 06 '14 at 14:16
2

The error message is telling you exactly what to do:

If Not Me.header Is otherType.header Then
    Return False
End If

The Is operator in VB.NET makes reference comparisons, exactly like == does in C# for reference types that have not overridden it (byte[] is such a type).

Jon
  • 428,835
  • 81
  • 738
  • 806
1

Looks like the converter has added in the "Offset" for some reason known only to its developers (or perhaps not even them) it should work as you put it at the end of your post.

If Not Me.header Is otherType.header Then
    Return False
End If

Or alternatively (I think this is a bit more readable):

If Me.header IsNot otherType.header Then
    Return False
End If
BenM
  • 4,218
  • 2
  • 31
  • 58