I have created one extension method for datarow which is like this, it can return primitive type only.
<Extension()>
Public Function ToNullableNumber(Of T As Structure)(dataRow As DataRow, columnName As String) As T
Dim result As T
Dim isNumebr As Boolean = IsNumber(dataRow, columnName)
result = If(isNumebr, dataRow(columnName), 0)
Return result
End Function
calling it like
Dim drUplodedFileRowas as DataRow = dataTableCustomer.Rows(0)
Dim paidAmount as Integer = drUplodedFileRow.ToNullableNumber(Of Integer)("PaidAmount")
this is fine for non nullable type but I want to call my ext. method like this also
Dim paidAmount as Integer? = drUplodedFileRow.ToNullableNumber(Of Integer?)("PaidAmount")
but it is giving error. "system.nullable does not satisfy the 'structure' constraint for type parameter 'T' only non-nullable ''Structure' types are allowed"
then I called it like this which also giving same error
Dim paidAmount as Nullable(Of Integer) = drUplodedFileRow.ToNullableNumber(Of Nullable(Of Integer))("PaidAmount")
can any one explain me how do I define my extension method so it should accept primitive nullable data type also.
You shoud check for DBNULL and return Nothing. Here is a sample:
And: