vb.net - NullReferenceException thrown even though checking for Nothing -
i have rather straightforward code compare function
public overridable function comparer(thisvalue object, othervalue object) integer try if thisvalue nothing if othervalue nothing return 0 else return -1 end if else if othervalue nothing return 1 else return thisvalue.tostring.compareto(othervalue.tostring) end if end if catch ex exception return 0 end try end function
the reason try-catch block is: nullreferenceexception @ actual comparision line if thisvalue nothing. debugger shows me thisvalue "nothing" lands in else branch anyway.
can tell me why?
update: have tried amend situation inserting yet check nothing. in scenario boils down few hundred exceptions , execution speed bearable. don't want imagine trying sort empty column though.
http://i.stack.imgur.com/8dnxd.png
how possible? there "level" of nothingness don't know about. need check type of thisvalue , othervalue?
the function never overridden. have tried removing "overridable" no effect.
perhaps isn't thisvalue
nothing
, fact .tostring()
returning nothing? try code test out:
public overridable function comparer(thisvalue object, othervalue object) integer try if thisvalue nothing if othervalue nothing return 0 else return -1 end if else if othervalue nothing return 1 else dim thisvaluestr string = thisvalue.tostring() dim othervaluestr string = othervalue.tostring() 'here, check 2 strings null!!! return thisvaluestr .compareto(othervaluestr ) end if end if catch ex exception return 0 end try end function
if case, double-check implementation of tostring()
in object being passed (assuming custom type).
Comments
Post a Comment