0

I have written the following code to do this but is there a more elegant way?

I have 2 nullable Enum's. I want to compare them to each other, where one or both can be null. I have to test separately for equality and test for the null condition. Is there a better way?

Private Class a
    Public Enum MyColour
        Red
        Blue
    End Enum
    Public Property OriginalColour As MyColour?
    Public Property NewColour As MyColour?

    Public ReadOnly Property HasColourChanged As Boolean
        Get
            If (OriginalColour.HasValue And NewColour.HasValue) Then  'Both have values so test
                'Test if the values are the same
                If OriginalColour.Value = NewColour.Value Then
                    Return False
                Else
                    Return True
                End If
            End If

            'Either one or both values are null
            If OriginalColour.HasValue Xor NewColour.HasValue Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property
End Class
  • 1
    Do you want to know when the property changes or if they are different? Because your property just indicates you want to know if the colour has changed – Bill Dinger Jan 07 '15 at 05:15
  • Just curious because if all you care about is notifying something that a color has changed you should check out INotifyPropertyChanged: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx – Bill Dinger Jan 07 '15 at 05:19
  • Duplicate of [How to compare nullable types?](http://stackoverflow.com/questions/2341660/how-to-compare-nullable-types) – Bjørn-Roger Kringsjå Jan 07 '15 at 06:35
  • Google `compare nullable` and click the second link. It's as simple as that. – Bjørn-Roger Kringsjå Jan 07 '15 at 06:37
  • Bill, I badly worded the property. I wanted to know if they were different. – Coding123456 Jan 08 '15 at 05:52

3 Answers3

2

You can use the Nullable.Equals method. Keeps things nice and simple.

Public ReadOnly Property HasColourChanged As Boolean
    Get
        Return Not Nullable.Equals(OriginalColour, NewColour)
    End Get
End Property
reduckted
  • 2,358
  • 3
  • 28
  • 37
  • The method you linked to is not `Shared` (`static` in C#), so it should be `OriginalColour.Equals(NewColour)`. – Heinzi Jan 07 '15 at 06:28
  • @Heinzi Oops, wrong method. Thanks for picking that up. I meant to link to `Nullable.Equals` but linked to `Nullable.Equals` by mistake. I've fixed the link. – reduckted Jan 07 '15 at 06:41
0

You are returning false only for one condition, hence make your property return true as default and return false only when your condition is met, that way you won't have to check for returning true. So imo you won't be needing so many conditions, following should suffice

Public ReadOnly Property HasColourChanged As Boolean
    Get
        If (OriginalColour.HasValue And NewColour.HasValue) Then  'Both have values so test
            'Test if the values are the same
            If OriginalColour.Value = NewColour.Value Then
                Return False                
        End If

        Return True
    End Get
End Property
Codeek
  • 1,624
  • 1
  • 11
  • 20
0

I'm not sure this adds to the readability, but I believe you can abbreviate this code using the short circuit AndAlso operator to avoid a potential unnecessary RHS check, and also use the Conditional Operator:

Public ReadOnly Property HasColourChanged As Boolean
    Get
        Return If (OriginalColour.HasValue AndAlso NewColour.HasValue, _
                   Not(OriginalColour.Value = NewColour.Value), _
                   OriginalColour.HasValue Xor NewColour.HasValue)      
    End Get
End Property

I tested with the following:

Dim test as New a
test.OriginalColour = MyColour.Red
test.NewColour = MyColour.Blue
Debug.Assert(test.HasColourChanged)

test.OriginalColour = MyColour.Blue
test.NewColour = MyColour.Blue
Debug.Assert(Not(test.HasColourChanged))

test.OriginalColour = Nothing
test.NewColour = MyColour.Blue
Debug.Assert(test.HasColourChanged)

test.OriginalColour = MyColour.Blue
test.NewColour = Nothing
Debug.Assert(test.HasColourChanged)

test.OriginalColour = Nothing
test.NewColour = Nothing
Debug.Assert(Not(test.HasColourChanged))
StuartLC
  • 104,537
  • 17
  • 209
  • 285