1

I have some rather straightforward code for a Compare Function

    Public Overridable Function Comparer(thisValue As Object, otherValue As Object) As Integer
    Try
        If thisValue Is Nothing Then
            If otherValue Is Nothing Then
                Return 0
            Else
                Return -1
            End If
        Else
            If otherValue Is Nothing Then
                Return 1
            Else
                Return thisValue.ToString.CompareTo(otherValue.ToString)
            End If
        End If
    Catch ex As Exception
        Return 0
    End Try
End Function

The reason for the try-catch block is: I get a NullReferenceException at the actual comparision line if thisValue is Nothing. The debugger shows me that thisValue is "Nothing" but lands in the ELSE branch anyway.

Can anyone tell me why?

UPDATE: I have tried to amend the situation by inserting yet another check for Nothing. In my scenario this boils down to a few hundred Exceptions and the execution speed is bearable. Don't want to imagine someone trying to sort an empty column though.

https://i.stack.imgur.com/8dnXD.png

How is this possible? Is there another "LEVEL" of Nothingness that i don't know about. Do i need to check the type of thisValue and otherValue?

The Function is never overridden. I have tried removing the "Overridable" to no effect.

themightylc
  • 304
  • 2
  • 15
  • Sorry but I cannot see how this code can deliver what you claim. Could you please post the exact input conditions you are testing? – varocarbas Aug 26 '13 at 08:05
  • 1
    Just did insert a picture - I felt like i had to proof my claim :) – themightylc Aug 26 '13 at 08:09
  • Logically, I believe your statement (what would be the point of this question otherwise?!); but I am not able to replicate your conditions. Please, input your exact input conditions. Bear in mind that Object is a pretty "peculiar" type which admits lots of configurations (and thus problems). Your pic looks certainly weird but I would need to know more about how this function is called (variables are getting the null) before saying anything. – varocarbas Aug 26 '13 at 08:13
  • Also I guess that you don't have Option Strict On; could you please write Option Strict On on the top of your file and tell what changes? – varocarbas Aug 26 '13 at 08:15
  • whoa... i thought strict was "gone" and thus "always" on. I have other things to attend to but I will return with another update in a couple of hours. The function is exclusively called from sorting the column of a DataTable. – themightylc Aug 26 '13 at 08:19
  • You can configure from your settings the default behaviour (http://stackoverflow.com/questions/5160669/option-strict-on-by-default-in-vb-net); but this error sounds like you made something not completely right at some point (allowed via Option Strict Off). – varocarbas Aug 26 '13 at 08:21

1 Answers1

3

Perhaps it isn't that thisValue is Nothing, it is the fact that .ToString() is returning Nothing? Try this code to test it out:

Public Overridable Function Comparer(thisValue As Object, otherValue As Object) As Integer
    Try
        If thisValue Is Nothing Then
            If otherValue Is Nothing Then
                Return 0
            Else
                Return -1
            End If
        Else
            If otherValue Is Nothing Then
                Return 1
            Else

                Dim thisValueStr As String = thisValue.ToString()
                Dim otherValueStr As String = otherValue.ToString()

                'HERE, CHECK THE TWO STRINGS FOR NULL!!!

                Return thisValueStr .CompareTo(otherValueStr )

            End If
        End If
    Catch ex As Exception
        Return 0
    End Try
End Function

If this is the case, double-check the implementation of ToString() in the object being passed (assuming it is a custom type).

tcarvin
  • 10,715
  • 3
  • 31
  • 52
  • 2
    Nice catch - it is exactly as you say. I've overlooked another test for Nothing in the implementation of ToString() (never trust external data, even if you created it yourself ;)) Thank you! Does that mean the (de)bugger shows the result of ToString() as the current value? (what else would it show...) – themightylc Aug 28 '13 at 09:28