2

I have a set of enum

public enum enums
{
    enum1,
    enum2,
    enum3,
    enum4,
    enum5
}

and in a another class I want to have two objects:

var object1 = new enums();//Here I want an enum of enum1,enum2
var object2 = new enums();//Here I want an enum of enum3,enum4,enum5

is there anyone with an idea on how I can do this?

John Willemse
  • 6,608
  • 7
  • 31
  • 45
Jack M
  • 2,564
  • 4
  • 28
  • 49
  • 5
    http://www.perlmonks.org/?node_id=542341 – I4V May 24 '13 at 08:25
  • 3
    Could you please explain why you want a single enum, but want to use only half of it? – Hans Kesting May 24 '13 at 08:27
  • Do you want the objects to only be able to hold those values (restriction) or do you want them to contain all the indicated values at once? There's a difference, of which the first cannot be accomplished in C# – John Willemse May 24 '13 at 08:31
  • What does this have to do with overriding? And why are people voting to close as "not constructive" instead of "not a real question"? – Cody Gray - on strike May 24 '13 at 09:21
  • Subject was overriding as I thought I will be doing something with new blabl(){....,....,....}; not contructive is their opinion as I faced this and wasn't sure which way to go. and not a real question, maybe to you, but to me it was a question. And by the way why are u bothered answering ? – Jack M May 24 '13 at 10:10

1 Answers1

7

Use Flags Attribute and you can assign more values:

[Flags]
public enum enums
{
    enum1 = 1,
    enum2 = 2,
    enum3 = 4,
    enum4 = 8,
    enum5 = 16
}

here is good post explaining how to use it: https://stackoverflow.com/a/8480/2043144

More edits:

then your code should look like that:

enums object1 = enums.enum1 | enums.enum2;
enums object2 = enums.enum3 | enums.enum4 | enums.enum5;

to check wether your object1 contains given flag:

if ((object1 & enums.enum1) == enums.enum1) 
{
    //object1 contains enums.enum1 flag
}
Community
  • 1
  • 1
gzaxx
  • 17,312
  • 2
  • 36
  • 54