5

I wrote:

    MfgRecipeTypeKey = If(placeholderMRTK Is Nothing, 0, placeholderMRTK)

and that's all fine, but when placeholderMRTK actually is Nothing, it fails, without raising an exception, just quitting the sub (MyBase.Load for a dialog form) and continuing on with the application. When I rewrite it as:

    If placeholderMRTK Is Nothing Then
        MfgRecipeTypeKey = 0
    Else
        MfgRecipeTypeKey = placeholderMRTK
    End If

it works fine. I thought the two were logical equivalents.

So:

1) What is the actual difference between those two that I don't know about?

2) Why might the first one fail? I sort of wonder if it's some screwy typecasting issue, but both placeholderMRTK and MfgRecipeTypeKey are declared as Byte? (nullable byte) types.

3) Why does execution just fall out of the sub without presenting me with an exception. When that line is highlighted in Visual Studio (Pro 2013 if it matters) and I f11 for next line, it just hops out and runs a datagrid rendering event and then presents my dialog, but without some important data assignments having taken place under the hood. And given that it does this (is this new behavior in 2013?), how am I supposed to be debugging?

Thanks for your time and attention!

clweeks
  • 856
  • 7
  • 31
  • If both variables are nullable instead of Is Nothing you can use HasValue method, for example: MfgRecipeTypeKey.Value = If (placeholderMRTK.HasValue, placeholderMRTK.Value, 0) – sblandin Jun 18 '15 at 15:06
  • 1
    As for your third question: [VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows](https://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a/11997142#11997142) – sloth Jun 18 '15 at 15:12
  • To find out why the first statement is failing, I suggest executing it (just as a test) sometime after the Load event handler has completed. That way you should be able to see the exception. – Blackwood Jun 18 '15 at 15:12

2 Answers2

4

The If() operator with three arguments you use expects that the two possible branches return a value of the same type.

It's not the case when you use

MfgRecipeTypeKey = If(placeholderMRTK Is Nothing, 0, placeholderMRTK)

since placeholderMRTK is of type Nullable(Of Byte) and 0 is of type Integer.


 If placeholderMRTK Is Nothing Then
     MfgRecipeTypeKey = 0
 Else
     MfgRecipeTypeKey = placeholderMRTK
 End If

works because VB.Net allows you the implicit conversion of 0 to Byte.


You could use

MfgRecipeTypeKey = If(placeholderMRTK Is Nothing, CType(0, Byte), placeholderMRTK)

converting the 0 into a Byte, or simply use

MfgRecipeTypeKey = If(placeholderMRTK, 0)
sloth
  • 99,095
  • 21
  • 171
  • 219
1

You could use the built-in function GetValueOrDefault.

MfgRecipeTypeKey = placeholderMRTK.GetValueOrDefault(0)
the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • 1
    I clearly should have researched nullable types before starting to use them for the first time. I have a boatload of inelegant code to spruce up now. This is great, thanks! – clweeks Jun 18 '15 at 15:42