简体   繁体   中英

Using an enum value to represent two enum values

Picture the scene.

public enum SaveStates
{
    Saved,               //Represents a successful save.
    SavedWithChanges,    //Represents a successful save, where records were modified
    SavedWithoutChanges  //Represents a successful save, but no records were modified
}

In this scenario, the enumeration can be considered Saved if it's SavedWithChanges or SavedWithoutChanges .

So if I were to have a variable like this:

SaveStates lastState = SaveStates.SavedWithoutChanges;

I'd ideally like to do something like this:

if (lastState == SaveStates.Saved)
{
    //The state is saved, do something awesome.
}

I can of course do this:

if (lastState == SaveStates.SavedWithChanges || lastState == SaveStates.SavedWithoutChanges)
{
    ...

However this is a bit tedious and I can't assume that another developer is going to understand how to correctly use the enumeration.

Each enumeration is required as there may be an instance where we might want to do something specific in the event of a save where there has been no changes for example.

I'm open to alternative design ideas.

If what worries you is code readability, you can use a little extension method like this:

public static class SaveStatesExtension
{
    public static bool IsSavedState(this SaveStates state) {
        return state == SaveStates.SavedWithChanges || 
               state == SaveStates.SavedWithoutChanges;
    }
}

Then your usage example becomes:

if (lastState.IsSavedState())
{
    //The state is saved, do something awesome.
}

Of course the Saved member in the enum is no longer needed in this case.

You can achieve this as follows, going by the example of the link I've posted under the question but using non-exclusive flag values. Note that both SavedWithChanges and SavedWithoutChanges contain the bit 1 , assigned to Saved .

[Flags]
public enum SaveStates
{
    Saved = 1,
    SavedWithChanges = 3,
    SavedWithoutChanges = 5
}

if ((lastState & SaveStates.Saved) == SaveStates.Saved)
{

}

However, this can be rather unintuitive to other developers - usually flag enums are not used this way. So explicitly stating all conditions with all enum values might be much more readable. Very good idea posted by Konamiman :

public static bool IsSaved(SaveStates state)
{
    return state == SaveStates.SavedWithChanges
        || state == SaveStates.SavedWithoutChanges;
}

combines best of two worlds: principle of least astonishment satisfied while being concise and readable.

If you're going to do it with a Flags enum, you should make it self-documenting

[Flags]
public enum SaveStates
{
    Saved = 1,
    WithChanges = 2,
    SavedWithoutChanges = Saved, // a bit pointless! Its the same as Saved
    SavedWithChanges = Saved | WithChanges  // has value "3"
}

And then, as per other answers

if ((lastState & SaveStates.Saved) == SaveStates.Saved)
{

}

What about changing your enumeration?

public enum SaveStates
{
    NotSaved,               //Represents "not saved" state
    SavedWithChanges,    //Represents a successful save, where records were modified
    SavedWithoutChanges  //Represents a successful save, but no records were modified
}

In this case you can use negation for the purpose you're speaking of:

if (lastState != SaveStates.NotSaved)
{
    //The state is saved, do something awesome.
}

Also, it gives you an enumeration value which can be used as the "default" one which is considered a good, "clean code" practice.

I would prefer this:

SaveStates[] savedWithOrWithoutChanges = { SaveStates.SavedWithChanges, SaveStates.SavedWithoutChanges };
if (savedWithOrWithoutChanges.Contains(lastStat))
{
    ...
}

This is very intuitive, every developer will understand it.

Instead of one enum to represent two things why not just use two bool values. One to indicate if it is saved and the other to indicate if it is "with changes". Then you'd just ignore the second if the first is false.

private bool saved;

private bool withChanges;

public void SomeMethod()
{
    if (saved)
    {
        Console.WriteLine("Saved");

        if (withChanges)
        {
            Console.WriteLine("With Changes");
        }
        else
        {
            Console.WriteLine("Without Changes");
        }
    }
    else
    {
        Console.WriteLine("Not saved");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM