PC Review


Reply
Thread Tools Rate Thread

How to compare enums?

 
 
nomad
Guest
Posts: n/a
 
      3rd Jul 2008
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.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      3rd Jul 2008
On Jul 3, 8:57*am, nomad <d.bedg...@btinternet.com> wrote:
> 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
 
Reply With Quote
 
nomad
Guest
Posts: n/a
 
      3rd Jul 2008
On 3 Jul, 09:02, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> On Jul 3, 8:57*am, nomad <d.bedg...@btinternet.com> wrote:
>
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
enums in mc++ Cartoper Microsoft VC .NET 4 13th Dec 2007 10:18 PM
Enums. What are they and Why use them =?Utf-8?B?am9obmI=?= Microsoft Access VBA Modules 4 3rd Apr 2007 01:31 AM
Enums and Arrays of Enums J055 Microsoft C# .NET 3 12th Sep 2006 11:31 PM
"global" enums vs. enums in class? Harold Hsu Microsoft VB .NET 1 21st Oct 2004 07:40 PM
enums martin Microsoft ASP .NET 2 28th May 2004 05:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:36 PM.