Hey, is there any way to store negative flags in C#? For example I have the following flags enum that represents some styles:
[Flags]
public enum Styles
{
Default = 0,
Bold = 1,
Italic = 2
}
Now I have multiple objects, those styles can be applied to, and later all related are combined (i.e. other object may inherit some previously set styles). In addition to that, I would like to define negative flags, that basically undo inherited styles. So if the style was previously set to Styles.Bold | Styles.Italic
and the object inherits that style but has a negative Styles.Bold
flag set, then the result should be just Styles.Italic
.
Is there any mechanism that already does this? I basically though of two ways now: First is defining NotXY
enum values, that are then somehow parsed to eliminate the XY
values if set.
The other is simply defining two fields, positive and negative flags, where the negative flags are specially defined in an extra field and I get the resulting flags by simply doing positiveFlags ^ negativeFlags
.
edit:
If this wasn't clear, I need to store each of those intermediate objects' styles. So it could be for example like this:
object1: Default
object2: Bold
object3: -Bold Italic
And if object3 also inherits the values of 1 and 2, then the final result should be just Italic
.
Another example in response to Kieren Johnstone's question, and per my statement in the comment that negative values only apply on the current level:
1: Bold
2: -Bold -Italic
3: Italic
2 eliminates both Bold and Italic from previous objects, but then shouldn't apply any further (positive values should though), so the final value would be Italic
again.