Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Settings with Flag Enums

Tags:

c#

.net

enums

I've enums as flags like:

[Flags]
public enum ResultValues
{
    None = 0,
    Red = 1,
    Yellow = 2,
    Green = 4,
    Other = 8
};

In my code I can OR-connect these flags and all works fine.

Now I store variables of this enum type in the settings. If I add a setting entry, I can set as type the enum from above and select/set one (!) of the values as default (e.g. Green).

<Setting Name="FilterResult" Type="MyNamespace.ResultValues" Scope="User">
  <Value Profile="(Default)">Green</Value>
</Setting>

But how can I store a combination of values as default - as I can do this in code with

ResultValues.Red | ResultValues.Yellow | ResultValues.Green | ResultValues.Other
like image 808
Konrad Avatar asked Dec 07 '22 11:12

Konrad


2 Answers

You can store the values just by separating them with a comma:

<Value Profile="(Default)">Green, Other</Value>

The built-in parser will convert such a string to the correct "combined" enum value:

ResultValues.Green | ResultValues.Other
like image 131
dymanoid Avatar answered Dec 30 '22 15:12

dymanoid


Comma separated:

Flags f = Enum.Parse<Flags>("Red,Yellow,Green");
like image 20
Reno Avatar answered Dec 30 '22 16:12

Reno