Today I stumbled upon the weird behavior of the VB.net If () operator. Perhaps you can explain why it works as if it is possible, or maybe you can confirm that this is a mistake.
So, I have an SQL database with a table "TestTable" with an int column "NullableColumn", which may contain NULL. I would like to read the contents of this column.
So, I declare a type variable Nullable(Of Integer), for this I need to open the SqlClient.SqlDataReader"SELECT NullableColumn FROM TestTable" and use the following code to get the contents of this column:
Dim content as Nullable(Of Integer)
...
Using reader as SqlClient.SqlDataReader = ...
content = If(reader.IsDBNull(reader.GetOrdinal("NullableColumn")), Nothing, reader.GetInt32(reader.GetOrdinal("NullableColumn")))
End Using
But after that, my variable contenthas a value of 0, and not Nothing, as I expected.
When debugging, everything looks good, so
reader.GetOrdinal("NullableColumn") ( 0)reader.IsDBNull(0) reader.IsDBNull(reader.GetOrdinal("NullableColumn")) True, NULLIf(1=2, Nothing, "Not Nothing") " "If(1=1, Nothing, "Not Nothing") Nothingreader.GetInt32(reader.GetOrdinal("NullableColumn")) , NULL Integer
, 0?