Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 none, one or both can be null. I have to test separately for equality and test for the null condition. Is there a better way?

VB
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 both have values test
             If (OriginalColour.HasValue And NewColour.HasValue) Then
                '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
Posted
Updated 6-Jan-15 18:20pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900