Home > Mobile >  Enum based on condition in Class
Enum based on condition in Class

Time:12-08

I currently have the following code:

public partial class Monster : BaseMonster
    {
        public bool IsOverhealed => Hp > MaxHp;
        public bool IsBloody => Hp <= MaxHp / 2.0;
        public bool IsNearDeath => Hp <= MaxHp / 4.0;
        public bool IsDead => Hp <= 0;
    }

I'd like to use these values to alter the color of HP. However, this is rather clunky, and I know that an Enum could be used, I am however not sure how.

I've created this Enum:

public enum HeathStateEnum
    {
        Overhealed,
        Healthy,
        Bloody,
        NearDeath,
        Dead
    }

And then the class should look something like this:

public partial class Monster : BaseMonster
        {
            public HealthStateEnum HealthState => ...
        }

But I am not sure how to properly assign the right value of the Enum to the HealthState, based on the conditions of the current code.

(Also, a little bonus question, what is it called when using => inside a Class?)

CodePudding user response:

little bonus question, what is it called when using => inside a Class?

That's called an expression bodied member, in this case read only property. Read.

I would use a method, that is clear, readable and maintainable:

public enum MonsterHealthState
{
    Overhealed,
    Healthy,
    Bloody,
    NearDeath,
    Dead
}

public partial class Monster : BaseMonster
{
    public MonsterHealthState HealthState => GetHealthState();

    private MonsterHealthState GetHealthState()
    {
        if (Hp > MaxHp) return MonsterHealthState.Overhealed;
        if (Hp <= 0) return MonsterHealthState.Dead;
        if (Hp <= MaxHp / 4.0) return MonsterHealthState.NearDeath;
        if (Hp <= MaxHp / 2.0) return MonsterHealthState.Bloody;
        return MonsterHealthState.Healthy;
    }
}

CodePudding user response:

You can use switch expression with case guards:

public partial class Monster 
{
    public int Hp { get; set; }
    public int MaxHp { get; set; }

    public HealthStateEnum HealthState => Hp switch
    {
        _ when Hp > MaxHp => HealthStateEnum.Overhealed,
        _ when Hp <= 0  => HealthStateEnum.Dead,
        _ when Hp <= MaxHp / 4.0 => HealthStateEnum.NearDeath,
        _ when Hp <= MaxHp / 2.0 => HealthStateEnum.Bloody,
        _ => HealthStateEnum.Healthy
    };
    
    // if needed keep the old API:
    public bool IsOverhealed => HealthState == HealthStateEnum.Overhealed;
}

Note that order of checks is important, cause the first which matches will return the value (i.e you want to check the lower bounds first).

Another approach is to expand this to ifs:

public HealthStateEnum HealthState
{
    get
    {
        if (Hp > MaxHp)
            return HealthStateEnum.Overhealed;
        if (Hp <= 0)
            return HealthStateEnum.Dead;
        if (Hp <= MaxHp / 4.0)
            return HealthStateEnum.NearDeath;
        if (Hp <= MaxHp / 2.0)
            return HealthStateEnum.Bloody;
        return HealthStateEnum.Healthy;
    }
}

Also, a little bonus question, what is it called when using => inside a Class?

It is expression bodied property - see the expression bodied members docs.

CodePudding user response:

You mean something like

public partial class Monster : BaseMonster
{
    public HeathStateEnum HeathState
    {
        get
        {
            if (Hp > MaxHp) return HeathStateEnum.Overhealed;
            if ( Hp <= MaxHp / 4.0) return HeathStateEnum.NearDeath;
            ...
        }
    }

}

?

  • Related