Home > Net >  VB.net value 1 showing as a VBNull
VB.net value 1 showing as a VBNull

Time:10-08

dim MyStr as string="1"
If  Convert.toInt32(MyStr)=vbNull Then
System.Console.WriteLine("Null")
Else
System.Console.WriteLine("Not Null")
End If

dim Myval as integer="1"
If  Myval=vbNull Then
   System.Console.WriteLine("Null")
Else
   System.Console.WriteLine("Not Null")
End If

This Code returns 'null' even it has a value. If i put '2' as value it is showing 'not null', Can anyone explan why this is showing 'null'.

CodePudding user response:

There's a lot wrong there but addressing the specific issue raised, you are confused about what vbNull actually is. If you mouse over it in code, you'll see that it is a constant with the value VariantType.Null. VariantType is an enumeration and, like all enumerations, its fields have numeric values. Guess what the numeric value of the Null field is. That's right, it's 1.

In short, you shouldn't be using vbNull at all but it's not really possible to say what you should do instead because your question doesn't explain what you're actually trying to achieve. If you want to determine whether a variable has no value then you should be comparing it to Nothing. The thing is, for non-nullable value types like Integer, Nothing will correspond to the default value for that type, e.g. 0.

  • Related