Negative flags in C #

Hey, is there a way to keep negative flags in C #? For example, I have the following flag enumeration that represents some styles:

[Flags] public enum Styles { Default = 0, Bold = 1, Italic = 2 } 

Now I have several objects to which these styles can be applied, and then all the related ones are combined (i.e. another object can inherit some previously set styles). In addition to this, I would like to define negative flags that basically override the inherited styles. Therefore, if the style was previously set to Styles.Bold | Styles.Italic Styles.Bold | Styles.Italic , and the object inherits this style, but has a negative Styles.Bold flag, then the result should be just Styles.Italic .

Is there any mechanism that already does this? I basically, although with two methods: first defines NotXY enum values, which are then somehow analyzed to exclude XY values ​​if they are set.

The other is simply the definition of two fields, positive and negative flags, where negative flags are specifically defined in the optional field, and I get the resulting flags by simply doing positiveFlags ^ negativeFlags .

edit:

If this is not clear, I need to save each of these styles of intermediate objects. So it can be, for example, like this:

 object1: Default object2: Bold object3: -Bold Italic 

And if object3 also inherits the values ​​1 and 2, the end result should just be Italic .

Another example is in response to a question from Cyrene Johnston, and in my statement in a comment that negative values ​​apply only at the current level:

 1: Bold 2: -Bold -Italic 3: Italic 

2 eliminates both bold and italics from previous objects, but then should not be applied further (positive values ​​should be), so the final value will be Italic again.

+8
enums c # flags
source share
4 answers

So should negativity always take precedence? What if you start with NotBold and then add Bold ?

If it is always positive, overridden by negative, if present, I would suggest two fields:

 private Styles baseStyles; private Styles overrideDisableStyles; public Styles ResultsStyles { return baseStyles & ~overrideDisableStyles; } 

Or you can create a helper class with SetStyle and UnsetStyle :

 public void SetStyle(Styles styles) { this.styles |= styles; } public void UnsetStyle(Styles styles) { this.styles &= ~styles; } 

For reference, positiveFlags ^ negativeFlags will NOT work. Consider a situation where Bold not set, but NotBold is. The result will be Bold , although only the flag specified was NotBold .

Finally, if you are doing something commercial, in-depth, more complex than what you are describing here, and want to have inheritable inheritance of the extensible / extensible style type, you must create it correctly using the hierarchy of objects / classes - start by defining a Style , which has one or more parent objects. Each Style can have a set of name / value pairs of properties that override anything from the parent object - there can be a collection of Properties that returns these overrides, and an Evaluate method that starts with the root object and goes to the style in question, creating The resulting property list for you.

Hope this helps.

+8
source share

I would recommend going with a second approach. It seems to me more obvious and simpler.

+2
source share

The other is just the definition of two fields, positive and negative flags, where negative flags are specifically defined in the optional field, and I get the flags received just by doing positiveFlags ^ negativeFlags .

This seems most natural to me (except for the shoud read positiveFlags & ~negativeFlags expression). In addition, this leads to a simple method of applying legacy styles. If we rewrite your example

 1: Bold 2: -Bold -Italic 3: Italic 

as (no special syntax ...)

 1: pos:{ Bold } neg:{ none } 2: pos:{ none } neg:{ Bold Italic } 3: pos:{ Italic } neg:{ none } 

then effective styles are computed with inheritance as (prevLevelStyle | pos) & ~neg , resulting in:

 1: ({ none } | { Bold }) & ~{ none } = { Bold } 2: ({ Bold } | { none }) & ~{ Bold Italic } = { none } 3: ({ none } | { Italic }) & ~{ none } = { Italic } 

optional.

+1
source share

If I'm not mistaken, you don’t have to define negative flags, but use “positive” flags to cancel them.

For example, this code can be used to cancel the highlighted bold flag.

 Styles styles = Styles.Bold | Styles.Italic; styles = styles ^ Styles.Bold; 
0
source share

All Articles