How to compare enums?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nomad

    How to compare enums?

    Hi,

    I have an enum which obviously has several values. I want to be able
    to do a comparison to see if the value selected by a user is the same
    as one of the enums in the list. My code is below.

    if (risk.Vehicle.I mmobiliserMake != ImmobiliserMake .None ||
    risk.Vehicle.Im mobiliserMake != ImmobiliserMake .NA)

    Although the risk.Vehicle.Im mobiliserMake is equal to None, it still
    gioes into my if block. Any ideas where I am going wrong?

    Appreciate the help.
  • Jon Skeet [C# MVP]

    #2
    Re: How to compare enums?

    On Jul 3, 8:57 am, nomad <d.bedg...@btin ternet.comwrote :
    I have an enum which obviously has several values.  I want to be able
    to do a comparison to see if the value selected by a user is the same
    as one of the enums in the list.  My code is below.
    >
    if (risk.Vehicle.I mmobiliserMake != ImmobiliserMake .None ||
    risk.Vehicle.Im mobiliserMake != ImmobiliserMake .NA)
    >
    Although the risk.Vehicle.Im mobiliserMake is equal to None, it still
    gioes into my if block.  Any ideas where I am going wrong?
    Look carefully at your logic. You've said if it's not "none" *or* it's
    not "NA", then go into the block. In other words, it would have to be
    *both* "none" and "NA" to skip the block.

    Basically you want to change || to &&.

    Jon

    Comment

    • nomad

      #3
      Re: How to compare enums?

      On 3 Jul, 09:02, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      On Jul 3, 8:57 am, nomad <d.bedg...@btin ternet.comwrote :
      >
      I have an enum which obviously has several values.  I want to be able
      to do a comparison to see if the value selected by a user is the same
      as one of the enums in the list.  My code is below.
      >
      if (risk.Vehicle.I mmobiliserMake != ImmobiliserMake .None ||
      risk.Vehicle.Im mobiliserMake != ImmobiliserMake .NA)
      >
      Although the risk.Vehicle.Im mobiliserMake is equal to None, it still
      gioes into my if block.  Any ideas where I am going wrong?
      >
      Look carefully at your logic. You've said if it's not "none" *or* it's
      not "NA", then go into the block. In other words, it would have to be
      *both* "none" and "NA" to skip the block.
      >
      Basically you want to change || to &&.
      >
      Jon
      Thanks Jon, that did the trick.

      Comment

      Working...