1

even when I'm completely aware of why the CA1008 warning exists I don't know how to avoid it in the following situation. I have a Flag enum with the following meanings:

ValidValue = 0x01
WrittenValue = 0x02

So in this case 0 means InvalidValueNonWritten instead of None. The rule says

Do not suppress a warning from this rule except for flags-attributed enumerations that have previously shipped.

In this case I've not shipped the enum so how can I prevent this warning?

EDIT:

The warning explicitily says:

Warning 86 CA1008 : Microsoft.Design : In enum 'XXX', change the name of 'XXX.InvalidValueNonWritten' to 'None'.

EDIT 2:

More states of the enum:

CommandValue = 0x04 // Otherwise it is DataValue
InmediateValue = 0x08 // Otherwise it is Deferred
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207

2 Answers2

5

At this particular moment, the 0 value has a "not valid or written" meaning. However, this will no longer be the case if you add more enum members. For example, if you add ApprovedValue = 0x04, 0 will start to mean "not valid or written or approved". This is the main reason for always using a None name for the 0 value.

If None doesn't make sense as a name, this usually signals a flaw in the enum design or name. In your case, it sounds like the enum actually represents steps that the value has passed through, as opposed to state of the value (for which a flags enum wouldn't typically be used). Might the following be a closer representation of what you intend (where an associated class might have a property named something like CompletedValueProcessingSteps)?

[Flags]
enum ValueProcessingSteps
{
    None = 0,
    Validation = 1,
    Writing = 2
}
Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • First of all thanks for trying, I appreciate. Second one the guess is not right, they are completely different states and no sequential ones. A value can be in more than one of these states at the same time and I understand that 0 should be "not set" but then I should find a way to represent the value currently represented by 0. In any case I think that any flags enum will have the same problem, the 0 has always a meaning other than "none" don't you think? – Ignacio Soler Garcia Mar 28 '14 at 15:07
  • I added some more states to make more clear that this is a flag enumeration. – Ignacio Soler Garcia Mar 28 '14 at 15:10
  • I wasn't assuming the steps were sequential, and I wasn't suggesting that a non-flags enum be used instead. I simply hadn't shown the `FlagsAttribute` on the sample code. If the "steps" name bothers you, simply remove it from the name. If "processing" isn't relevant, try to find a name that is relevant to how you use it but that still fits with a "none" 0 zero value. (That said, the items you added in your update kind of make me suspect that these shouldn't all be in a single enum.) – Nicole Calinoiu Mar 28 '14 at 17:01
  • Sorry, now I get your point. It is just a different naming schema that will lead to have a valid None value. I'll check thanks. – Ignacio Soler Garcia Mar 31 '14 at 06:38
0

If 0 means InvalidValueNonWritten then add InvalidValueNonWritten into the enum. Then this warning won't happen. If you really shouldn't have 0 in your enum suppress the warning in code and ensure you put the justification in for future reference.

If you are expecting an exception due to 0 not been valid it is better to perform the validation in your method using the enum. Also this is better for serialization, for example if you are using serialization on models the model will de-serialize with a missing member from the source.

Dreamwalker
  • 3,032
  • 4
  • 30
  • 60