2016-08-15 66 views
1

我正在尝试使用自定义函数来确定指标的值。问题是传递给函数的字段可以是NULL。我使参数的类型为空,但不会让我做任何比较。我不知道为什么。SSRS自定义函数和空值

错误是“自定义代码的第20行存在错误:[BC30452]运算符'<'未针对类型'System.Nullable(Of Single)'和'System.Nullable(Of Single)'定义“。

Public Function GetIndicator(ByVal HistBal As Nullable(of single),ByVal CurBal as NULLABLE(of single)) as integer 

Dim iReturn as integer 

if NOT HistBal.HasValue Then 
    iReturn =0 
else If HistBal< CurBal then 
    If (HistBal-CurBal)/HistBal <-.1 Then 
     iReturn= 2 'Green arrow 
    Else 
     iReturn= 4 'Up yellow 
    end if 
Else if HistBal=CurBal then 
    iReturn=0 'blank 
else 

    If (HistBal-CurBal)/HistBal <.1 Then 
     iReturn= 3 'Dn yellow 
    Else 
     iReturn= 1 'red arrow 
    end if 
End if 

return iReturn 

End Function 

UPDATE:返回#ERROR

代码:

Public Function GetIndicator(ByVal HistBal As Nullable(of single),ByVal CurBal as NULLABLE(of single)) as integer 

Dim iReturn as integer 

if Not HistBal.HasValue or Not CurBal.HasValue Then 
    iReturn =0 
else 
    iReturn= 1 
End if 

return iReturn 

End Function 

回答

0

Operator '<' is not defined for types 'System.Nullable(Of Single)' and 'System.Nullable(Of Single)'.

这是完全一样的消息称:显然System.Nullable(Of T)没有定义<运营商。

您已在检查是否HistBal.HasValue。只需检查CurBal.HasValue是否实现逻辑,该逻辑确定在其中一个或两个值为null时确定要返回的内容,然后在两个已知具有非空值的分支中处理HistBal.ValueCurBal.Value

If Not HistBal.HasValue Or Not CurBal.HasValue Then 
    Return 0 'presumably return 0 when either value is null   
End If 

If HistBal.Value < CurBal.Value Then 'work off the Single, not the nullable 
    '... 
+0

谢谢。以前从未使用过这个Nullable参数。 – user1612851

+0

它编译,但我从该函数返回#Error。这个函数在我把它设为可空参数之前就工作了。 – user1612851

+0

进展!这个'#Error'是一个不同的问题,涉及不同的代码。我不知道你做了什么或者你给了什么输入。你的代码目前没有检查'CurBal.HasValue',如果它是null,那么它会抛出一个异常并返回一个错误。 –