Reputation: 14927
What's the best way to handle this situation:
I have this generic function
Function FieldValue(Of T)(row As DataRow,fieldName As String) As T
Return If(row.IsNull(fieldName),Nothing,CType(row(fieldName),T))
End Function
in the special case where the field value is null and T is String, I want to return String.Empty instead of Nothing.
Upvotes: 1
Views: 124
Reputation: 19733
I think the easiest way is to provide a specific function for T = string:
Function FieldValue(row As DataRow ,fieldName As String) As String
Return If(row.IsNull(fieldName), String.Empty, CType(row(fieldName), String))
End Function
(on top of your existing generic function)
Here's another option:
Function FieldValue(Of T)(row As DataRow,fieldName As String) As T
If GetType(T) = GetType(String) And row.IsNull(fieldName) Then
Return CType(CType(String.Empty, Object), T)
Else
Return If(row.IsNull(fieldName), Nothing, CType(row(fieldName), T))
End If
End Function
Upvotes: 2