Vb.net Convert Integer DBNULL to 0 - error

MMM picture MMM · Sep 17, 2012 · Viewed 16.8k times · Source

I'm having this method:

Private Function convertInteger(intInteger As Object) As Integer

    If IsDBNull(intInteger) Then
        convertInteger = 0
    Else
        convertInteger = cInt(intInteger)
    End If

End Function

But it returns this error:

operator '=' is not defined for type 'integer' and type 'dbnull'

Im trying to convert a DBnull value to 0..

But the problem is that the value im trying to convert is not always DBnull.. so how should i handle this?

Answer

codingbiz picture codingbiz · Sep 17, 2012

Try this

Private Function convertInteger(intInteger As Object) As Integer

    If intInteger = DBNull.Value Then
        Return 0
    End If

    Return intInteger

End Function

As suggested by [Tim Schmelter], look into Nullable types