Assuming I have the following enum
[Flags]
enum Options
{
Option1 = 1 << 0,
Option2 = 1 << 1,
Option3 = 1 << 2
}
And I were to set a variable as follows
var options = 0;
options |= Options.Option1;
options |= Options.Option3;
// now options should equal Option1 + Option3
// I then store that single value in the database
myDatabase.Options.Submit(options);
How do I then parse “Options” in order to get the original values back?
public List<Options> ParseOptions(Options options)
{
// Not sure how to parse the options.
}
You could use the
Enum.HasFlag()method to see if a particular flag is set. Just go through all the individual values testing if it is set.Note that the
HasFlag()method was added to .NET 4. It is however logically equivalent to this: