2016-08-15 63 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,那麼它會拋出一個異常並返回一個錯誤。 –