Error?? If you assign a value to an integer with a zero value through the ternary operator, it cannot become zero

dim val1 As Integer? = If(5 > 2, Nothing, 43)
' val1 = 0

dim val1 As Integer? = If(5 > 2, Nothing, Nothing)
' val1 = Nothing

What gives? Is this a mistake, or am I missing something?

+2
source share
1 answer

The problem is that it Nothingworks differently in VB.NET than, for example, nullin C #. When Nothingused in the context of a value type (for example Integer), it represents the default value of that type. In this case, 0

In the first example, both branches of the ternary operator are valid Integer. The true branch represents 0, and the false branch represents 43.

Integer, VB.NET , Object, Integer.

, , , Integer?, Integer Object. :

dim val1 As Integer? = If(5 > 2, Nothing, New Integer?(43))

an Integer?, Nothing Integer.

+13

All Articles