vb.net - Why is my Nullable(Of Int32) = 0 after I set it to Nothing? -
i think i'm missing fundamental nullable types. example open new understanding, @ least, maybe can 1 thing working right.
in class (a dialog form), declare:
property productstructureheaderkey int32?
in class, declare instance of dialog , attempt set property line:
dr.productstructureheaderkey = if(parentrow.cells(1).value nothing, nothing, int32.parse(parentrow.cells(1).value))
when line assigns nothing property, property equal 0. (and later, it's passing 0 db when want passing null.)
that's not expect , keep finding code (so, msdn, etc) looks i'm doing things right, clearly, i'm not. so, friends, doing wrong? how employ nullable types meet needs?
that's 1 of differences between c# , vb.net. in vb.net nothing
not mean null
default
. assigning default value of int32
property 0. caused if
-operator has infer type 2 values not property want assign.
instead use either if...else
:
if parentrow.cells(1).value nothing dr.productstructureheaderkey = nothing ' it's not 0 nothing else dr.productstructureheaderkey = int32.parse(parentrow.cells(1).value) end if
or force nullable new nullable(of int32)
:
dr.productstructureheaderkey = if(parentrow.cells(1).value nothing, new nullable(of int32), int32.parse(parentrow.cells(1).value))
further read: why there difference in checking null against value in vb.net , c#?
Comments
Post a Comment