locked
A question about null value (nothing) RRS feed

  • Question

  • I have a question about If statements when null (nothing) is involved.

    The following code does not work.  If Hours contains null then the code never returns a 0 in hours.

    Dim A = Me.DETAILs.Sum(Function(x) If(x.Hours, 0))

                If (Me.WORKORDER.Hours.GetValueOrDefault <> A) Then

                    Me.WORKORDER.Hours = A

                    If Me.WORKORDER.Hours Is Nothing Then

                        Me.WORKORDER.Hours = 0

                    End If

                End If

    However, the following code does work.

    Dim A = Me.DETAILs.Sum(Function(x) If(x.Hours, 0))

    If Me.WORKORDER.Hours Is Nothing Then

                        Me.WORKORDER.Hours = 0

                  End If

                If (Me.WORKORDER.Hours.GetValueOrDefault <> A) Then

                    Me.WORKORDER.Hours = A

                End If

    I'm left to assume that if Hours contains null (nothing) then a comparison to another field that contains a value is an invalid comparrison.  Is that correct?  Is that why the test of Hours for the "nothing" when it is nothing appears to work while a test for Hours against another field does not work when Hours is null?   

    Sunday, December 1, 2013 2:09 PM

Answers

  • The expression

    Me.WorkOrder.Hours <> A

    will never resolve to True if Hours is Nothing. You can avoid this by using the GetValueOrDefault method or checking for Nothing before doing the inequality comparison.

    When using reference types, you cannot avoid checking for Nothing. A nullable integer is a reference type. The only way to avoid having to check for Nothing is to not use reference types, but that is unrealistic in most applications especially since String is a reference type.


    Justin Anderson, LightSwitch Development Team

    • Marked as answer by Jyuma1 Tuesday, December 3, 2013 1:09 PM
    Tuesday, December 3, 2013 8:06 AM
    Moderator

All replies

  • First, I'll say no matter what code you are writing, always null/Nothing check variables and members before you try to access them.

    Second, you two blocks on code do completely different things. The reason why Me.WORKORDER.Hours is rarely 0 when it initially was Nothing is because you assign the value of A to it before you compare it to Nothing. So, it'll only end up being 0 when:

    1. Me.WORKORDER.Hours is 0 and A is 0
    2. Me.WORKORDER.Hours does not equal A and A is 0 or Nothing

    On a separate note, I would recommend using parentheses when calling a method name (in your case, GetValueOrDefault), even if it doesn't take arguments, to make it easier to read and to make it more explicit what GetValueOrDefault is.


    Justin Anderson, LightSwitch Development Team

    Monday, December 2, 2013 8:30 AM
    Moderator
  •  A clarification please... Is the following code invalid when Hours is null?

    If Me.WorkOrder.Hours <> A then

    Me.WorkOrder.Hours = A

    End If

    The code doesn't throw an error... it just doesn't resolve True so it doesn't make Me.WorkOrder.Hours equal to A.

    On a side note: Would it then be considered "best practices" to never allow nulls in order to avoid the need to be constantly checking for nothing?

    Thank you Justin.

       

    Monday, December 2, 2013 10:44 AM
  • The expression

    Me.WorkOrder.Hours <> A

    will never resolve to True if Hours is Nothing. You can avoid this by using the GetValueOrDefault method or checking for Nothing before doing the inequality comparison.

    When using reference types, you cannot avoid checking for Nothing. A nullable integer is a reference type. The only way to avoid having to check for Nothing is to not use reference types, but that is unrealistic in most applications especially since String is a reference type.


    Justin Anderson, LightSwitch Development Team

    • Marked as answer by Jyuma1 Tuesday, December 3, 2013 1:09 PM
    Tuesday, December 3, 2013 8:06 AM
    Moderator
  • Thank you.

    Tuesday, December 3, 2013 1:09 PM