5

In C#, I am trying to "add" values to a parameter that accepts enumerated flags. I can do it on one line with a bitwise operator "|", but I can't seem append to the parameter in a loop.

I have the following Enum specified as Flags.

[Flags]
public enum ProtectionOptions
{
  NoPrevention = 0,
  PreventEverything = 1,
  PreventCopying = 2,
  PreventPrinting = 4,
  PrintOnlyLowResolution = 8
}

Now, I can easily use the following code to add flag values to the parameter:

myObj.Protection = ProtectionOptions.PreventEverything | ProtectionOptions.PrintOnlyLowResolution;

But, what I want to do, is to get a list of protection options from a CSV string (from the Web.Config), loop through them and add them to my myObj.ProtectionOptions property. I don't know how to do this in a loop without using the bitwise OR "|" operator. Here is what I am wanting to do:

string protectionOptionsString = "NoPrevention, PreventPrinting";
string[] protectionOptions = protectionOptionsString.Split(',');
foreach (string protectionOption in protectionOptions)
{
  myObj.Protection += (ProtectionOptions) Enum.Parse(typeof (ProtectionOptions), protectionOption.Trim());
}

Conceptually, this is what I want, but I can't "+=" the values in the loop to the parameter.

bigmac
  • 2,553
  • 6
  • 38
  • 61

4 Answers4

18

You don't need to split. Enum.Parse is capable of parsing multiple values if you use the [Flags] attribute on the enum definition, which you did. Just parse and use the |= operator to "add" the flags.

string protectionOptionsString = "NoPrevention, PreventPrinting";
myObj.Protection |= (ProtectionOptions)Enum.Parse(typeof(ProtectionOptions), protectionOptionsString);
glenneroo
  • 1,908
  • 5
  • 30
  • 49
Motti Shaked
  • 1,854
  • 15
  • 8
  • Perfect... that worked. Thanks! Two quick questions for you. One, out of curiosity, if I wanted "append" flags without using the built in Parse function, it it possible? Two, for my Enum, I have heard conflicting stories on whether I should use a 0 value and give it a name. If I do use the 0 value, should it be "None" or can it be anything? – bigmac Nov 29 '11 at 21:21
  • If the value is represented as a string you have to use Parse to convert to an instance of the enum. You should not use 0 unless it is None. The problem is that if programmers who use your enum check for the value they sometimes use 0 for comparison (which they shouldn't, but it's common). If 0 is a value rather than lack of value, then if ((currentWeather & Cloudy) != 0) to test whether the flag has Cloudy when Cloudy is defined as 0 will return false instead of the expected true. I mention that in much detail in my tutorial. Follow the link I specified in the answer. – Motti Shaked Nov 29 '11 at 21:28
4

Use the |= operator.

myObj.Protection |= (ProtectionOptions) Enum.Parse(typeof (ProtectionOptions), protectionOption.Trim());
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

Why can't you do this?

string protectionOptionsString = "NoPrevention, PreventPrinting";
string[] protectionOptions = protectionOptionsString.Split(',');
foreach (string protectionOption in protectionOptions)
{
  myObj.Protection |= (ProtectionOptions) Enum.Parse(typeof (ProtectionOptions), protectionOption.Trim());
}

Alternatively, you could store the integer equivalent of the enumeration ((Int32)myObj.Protection) and load it as an integer as well.

David Schwartz
  • 1,956
  • 19
  • 28
  • Thanks Zelda, that answers my first question posted to Motti. I accepted his answer as it's cleaner for my needs right now, but that clears up that I need to use a "|=" instead of "+=" to append. I appreciate it! – bigmac Nov 29 '11 at 21:23
0

You want to use the compound OR assignment operator.

string protectionOptionsString = "NoPrevention, PreventPrinting";
string[] protectionOptions = protectionOptionsString.Split(',');

foreach (string protectionOption in protectionOptions)
{
  myObj.Protection |= (ProtectionOptions)Enum.Parse(typeof(ProtectionOptions), 
                                                    protectionOption.Trim());
}
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343