Is it possible to limit public enumeration values ​​in C #?

I am currently writing a tour program consisting of exhibits. The object of exposure at any given point is in one of four states defined by the ExhibitStates enumeration:

private enum ExhibitState { Ready, Active, Complete, Inactive };

For developers who will create exhibits, there are only two “start” states that I want them to be able to choose from:

public enum StartingExhibitState { Ready, Inactive };

Currently, I am set up so that after initialization, the exhibit immediately sets its state in accordance with its initial state, for example:

        switch (startingState) {
            case StartingExhibitState.Ready:
                SetState(ExhibitState.Ready);
                break;
            case StartingExhibitState.Inactive:
                SetState(ExhibitState.Inactive);
                break;
        }

I thought it was best practice today. Is there a better way to limit which enumeration options are public and which are private? Or is it better to just have two separate listings?

Thank you so much for your time.

+4
2

-

public enum ExhibitState 
{ 
    Inactive = 0,
    Active = 1,
    Ready = 2,
    Complete = 3
};

public enum InitialStates 
{ 
    Inactive = ExhibitState.Inactive,
    Ready = ExhibitState.Ready
};

public void SetInitial(InitialStates state)
{
    SetState((ExhibitState)state);
}

, .

public sealed class InitialState
{
    public static readonly InitialState Initial = new InitialState(ExhibitState.Initial);

    public static readonly InitialState Ready = new InitialState(ExhibitState.Ready);

    public ExhibitState State { get; }            

    private InitialState(ExhibitState state)
    {
        State = state;
    }
}

private, . sealed, .

public void SetInitial(InitialState start)
{
    SetState(start.State);
}  

// use it
SetInitial(InitialState.Initial);
SetInitial(InitialState.Ready);

, InitialState.

+4

( ) :

public abstract class ExhibitState 
{
    public static ExhibitInitialState Ready { get { return new ExhibitReadyState(); } }
    public static ExhibitInitialState Inactive { get { return new ExhibitInactiveState(); } }
    public static ExhibitState Complete { get { return new ExhibitCompleteState(); } }
    public static ExhibitState Active { get { return new ExhibitActiveState(); } }

    private class ExhibitReadyState : ExhibitInitialState {}
    private class ExhibitInactiveState : ExhibitInitialState {}
    private class ExhibitCompleteState : ExhibitState {}
    private class ExhibitActiveState : ExhibitState {}
}

public abstract class ExhibitInitialState : ExhibitState {}

. , get, , .

, ExhibitState.Ready . , ExhibitInitialState , :

public void SetInitial(ExhibitInitialState initState) { ... }

, @Fabio, , . , : , . , ExhibitState switch, , , , enum.

+2

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


All Articles