By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,456 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,456 IT Pros & Developers. It's quick & easy.

How to compare enums?

P: n/a
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.
Jul 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jul 3, 8:57*am, nomad <d.bedg...@btinternet.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.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
Jul 3 '08 #2

P: n/a
On 3 Jul, 09:02, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 3, 8:57*am, nomad <d.bedg...@btinternet.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.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
Thanks Jon, that did the trick.
Jul 3 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.