c# - Parsing an enum with the Flags attrubute not giving expected value -


my enum:

        [flags]         public enum equalityoperator         {             equal,             notequal,             lessthan,             lessthanorequal,             greaterthan,             greaterthanorequal,             like,             notlike,             in,             notin         } 

my code parsing it:

var operatorval = (equalityoperator)enum.parse(typeof (equalityoperator), filterinfo[3]); 

when debug, can see filterinfo[3] "like"

however, operatorval comes out "lessthan | greaterthan"

what missing? can not parse enums flags attribute?

you need specify values:

[flags] public enum equalityoperator {     equal = 0,     notequal = 1,     lessthan = 2,     lessthanorequal = 4,     greaterthan = 8,     greaterthanorequal = 16,     = 32,     notlike = 64,     in = 128,     notin = 256  } 

the reason like parsing lessthan | greaterthan because you've defined it, lessthan has value 2 , greaterthan has value 4. if take bitwise-or of these, end lessthan | greaterthan = 6. look, like has value 6 you've defined enum! so, did parse "correctly".

i'll frank though, don't see point of marking enum flags though. point of flags can bitwise operations on enum values. why think need bitwise operations on values of enum?


Comments

Popular posts from this blog

How to logout from a login page in asp.net -

Stack level too deep error after upgrade to rails 3.2 and ruby 1.9.3 -