How to compare enums?

  • Thread starter Thread starter nomad
  • Start date Start date
N

nomad

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.ImmobiliserMake != ImmobiliserMake.None ||
risk.Vehicle.ImmobiliserMake != ImmobiliserMake.NA)

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

Appreciate the help.
 
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.ImmobiliserMake != ImmobiliserMake.None ||
risk.Vehicle.ImmobiliserMake != ImmobiliserMake.NA)

Although the risk.Vehicle.ImmobiliserMake 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
 
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.
 
Back
Top