Using the enum value to represent two enum values

Draw a 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 case, the enumeration can be considered Savedif it is SavedWithChanges or SavedWithoutChanges .

So, if I had such a variable:

SaveStates lastState = SaveStates.SavedWithoutChanges;

Ideally, I would 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 little tedious, and I cannot assume that another developer will understand how to use the enumeration correctly.

Each listing is required, as there may be an instance where we might want to do something specific in case of saving, where, for example, there were no changes.

I am open to alternative design ideas.

+4
source share
6 answers

, , :

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

:

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

, Saved .

+5

, , , . , SavedWithChanges SavedWithoutChanges 1, Saved.

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

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

}

- . , . Konamiman:

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

: , .

+3

Flags,

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

,

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

}
+3

?

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
}

, :

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

, , " ", " ".

+1

:

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

This is very intuitive, every developer will understand it.

0
source

Instead of a single listing, to represent two things, why not just use two values bool. One to indicate whether it is saved, and the other to indicate whether it is “modified”. Then you simply 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");
    }
}
0
source

Source: https://habr.com/ru/post/1611769/


All Articles