How to make extension method which should returns generic type but only nullable numeric datatype in vb.net

201 views Asked by At

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.

1

There are 1 answers

1
Yuriy A. On

You shoud check for DBNULL and return Nothing. Here is a sample:

Sub Main()

        Dim table As DataTable
        table = MakeTable()

        Dim dr As DataRow = table.NewRow
        'dr("NullIntValue") = 
        dr("NotNullIntValue") = 5

        ' value1 = 5
        Dim value1 As Integer = dr.ToNullableNumber(Of Integer)("NotNullIntValue")

        ' value2 = Nothing
        Dim value2 As Integer? = dr.ToNullableNumber(Of Integer)("NullIntValue")

    End Sub

And:

Module StringExtensions

    <Extension()>
    Public Function ToNullableNumber(Of T As Structure)(dataRow As DataRow, columnName As String) As Nullable(Of T)
        Dim result As T

        If IsDBNull(dataRow(columnName)) Then
            Return Nothing
        Else
            Dim isNumebr As Boolean = IsNumber(dataRow, columnName)
            result = If(isNumebr, dataRow(columnName), 0)
            Return result
        End If

    End Function

    <Extension()>
    Public Function IsNumber(dataRow As DataRow, columnName As String) As Boolean

        Dim result As Int64
        Dim str As String = CType(DataRow(columnName), String)

        Return Int64.TryParse(str, result)

    End Function

End Module