开发者

wrong result with IF() function and a nullable integer?

I would expect the following vb.net function to return a value of Nothing, but instead its returning a value of 0...

Public Shared Function GetDefaultTipoSrvTkt() As Integer?
    Dim tsrvDict As New Dictionary(Of Integer, DataRow) 
    GetDefaultTipoSrvTkt = If(IsNothing(tsrvDict) OrElse 开发者_Go百科tsrvDict.Count = 0, Nothing, tsrvDict.First.Key)
End Function

The last line of the function could also be written as Return If(IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, Nothing, tsrvDict.First.Key) but in any case, why is the IF() function If(IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, Nothing, tsrvDict.First.Key) returning 0 instead of Nothing?


Nothing in VB can be applied to value types as well as reference types, and in all cases means "default value of this type". So, for example:

Dim x As Integer = Nothing
Console.WriteLine(x) ' 0

For If() operator, VB has to deduce the return type somehow. It looks at both branches, and figures out the nearest common type for them. In this case, one branch is of type Integer. Another is Nothing, which is "typeless", but it is compatible with Integer, as we saw earlier. Therefore, the result type of If() is deduced to be Integer. When Nothing is returned in that context, it becomes 0.

An explicit cast will fix this:

GetDefaultTipoSrvTkt = If( _
    IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, _
    CType(Nothing, Integer?), _
    tsrvDict.First.Key)

or you can use an alternative way to specify the null value for a nullable type:

GetDefaultTipoSrvTkt = If( _
    IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, _
    New Integer?(), _ 
    tsrvDict.First.Key)


I think Rodolfo has simplified the real code to make a short question, which is of course a good idea. But it is a little odd.

  • Dim x As New Y is equivalent to Dim x As Y: x = New Y, so a new dictionary is created every time the statement is executed. So the function always returns Nothing, and the dictionary never gets any entries, and goes out of scope immediately when the function returns.

Pavel's answer is probably the most helpful. But it is complex enough that I would just write a normal If statement

If tsrvDict Is Nothing OrElse tsrvDict.Count = 0 Then  
  Return Nothing  
Else   
  Return tsrvDict.First.Key   
End If 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜