1

I think I'm missing something fundamental about nullable types. Hopefully this example will open up new understanding, but at the very least, maybe we can get this one thing working right.

In a Class (a dialog form), I declare:

Property ProductStructureHeaderKey As Int32?

In another Class, I declare an instance of that dialog and attempt to set that Property with this line:

    dr.ProductStructureHeaderKey = If(parentRow.Cells(1).Value Is Nothing, Nothing, Int32.Parse(parentRow.Cells(1).Value))

When that line assigns Nothing to the property, the property is equal to 0. (And then later, it's passing 0 to the DB when I want it passing NULL.)

That's not what I expect and I keep finding code (SO, MSDN, etc) that looks like I'm doing things right, but clearly, I'm not. So, friends, what am I doing wrong? How do I employ Nullable types to meet my needs?

clweeks
  • 856
  • 7
  • 31
  • `DBNull` is not the same as `Nothing`; it might help to see the "later" part which should probably pass `DBNull` – Ňɏssa Pøngjǣrdenlarp Jun 25 '15 at 14:24
  • I have code elsewhere that handles Nothing to NULL conversions, the problem really was getting this Nullable set to Nothing. But Tim's answer directed me to my dumb mistake. – clweeks Jun 25 '15 at 14:33

1 Answers1

4

That's one of the differences between C# and VB.NET. In VB.NET Nothing does not only mean null but also default. So you are assigning the default value of Int32 to the property what is 0. This is caused by the If-operator that has to infer the type from the two values not from the property that you want to assign.

Instead use either an If...Else:

If parentRow.Cells(1).Value Is Nothing Then
    dr.ProductStructureHeaderKey = Nothing ' Now it's not 0 but Nothing
Else
    dr.ProductStructureHeaderKey = Int32.Parse(parentRow.Cells(1).Value)
End If

or force the nullable with new Nullable(Of Int32):

dr.ProductStructureHeaderKey = If(parentRow.Cells(1).Value Is Nothing, new Nullable(Of Int32), Int32.Parse(parentRow.Cells(1).Value))

Further read: Why is there a difference in checking null against a value in VB.NET and C#?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Arg! Unless I'm missing something, I should actually use `= If(parentRow.Cells(1).Value, Nothing)` I keep forgetting this one complication of using the three-part if() -- so frustrating. – clweeks Jun 25 '15 at 14:32
  • I don't know what `parentRow` is. – Tim Schmelter Jun 25 '15 at 14:35
  • Only if `parentRow.Cells(1).Value` returns a `Nullable(Of Int32)`, otherwise it doesn't compile with Option Strict On. It must be convertibe to `Int32?` implicitly. – Tim Schmelter Jun 25 '15 at 14:37
  • Oh, I see. When there's a value, it's a string, but it came from a SQL Int, so I know it's either always going to parse or be Nothing. So yeah, your `new Nullable(Of Int32)` is exactly what I need. Thanks! – clweeks Jun 25 '15 at 14:45