comparing two enums variables from two different enum classes.

i have two enum clasess
enum1
{
   NoAccess = 0,
   ReadOnly = 1,
   ViewAll = 2,
}

enum2
{
  InboxAccess =0,
  OfferAccess=1,
  OrderAccess=2
}

Now i am trying to comapre is like
if (enum2.InboxAccess == enum1.NoAccess)

But this code is throwing an error
Operator '==' cannot be applied to operands of type 'System.Enum' and enum1.NoAccess
pankajsinghpolarisAsked:
Who is Participating?
 
DhaestCommented:
Can you try this:

if ((int)enum2.InboxAccess == (int)enum1.NoAccess)
0
 
felipe_schauenburgCommented:
First, as the name suggests ENUMS are not classes, they are enumerations.
They are words, which can also be accesed sequentially by ints an Dhaest suggests, that makes programming more linguistical and undestandable. It really makes no much sense comparing to enums for equality as semanticaly they probably mean things completely different. But if you would like to do so, casting as is above suggested should do the deal.
0
 
MathiyazhaganCommented:
Hi,
It is not at all possible to compate two different Enums and you should never do with integer comparison.then will always true for following,


   InboxAccess= NoAccess  =0
   OfferAccess = ReadOnly  =1
   OrderAccess =    ViewAll = 2

so, it will change the indent of enumaration. you should do it like
create variables for enum1 and compare . no need of enum2.

//declare
 enum1  InboxAccess =enum1.ViewAll   ;
 //enum variable always intialized with first value automatically.change if needed
 enum1   OfferAccess ;
 enum1   OrderAccess;

//let user to assign the rights from UI/Data base...

compare:

if (InboxAccess =enum1.ViewAll ) {//code to veiwall rights}
else if (InboxAccess =enum1.ReadOnly  ) {//code to ReadOnly  rights}
else (InboxAccess =enum1.NoAccess  ) {//code to restrict rights}

do the same for OfferAccess and OrderAccess.use switch-case for better readablity
0
 
lazyberezovskyCommented:
What you are trying to do is very bad code style.
First, code you are writing makes no sense. What you are trying to compare? Enum is a list of names (you never should take care of underlying value). Underlying int value could be changed over time (e.g. someone decide to order names alphabetically).

If you have two objects - one of type Enum1 and other of type Enum2 (also very bad naming - consider names like AccessType), and you want two conditions at same time, you should do following:
if ( (e1 == Enum1.NoAccess) && (e2 == Enum2.InboxAccess))
0
 
pankajsinghpolarisAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.