Determining if enum value is in list (C#)

后端 未结 6 2317
抹茶落季
抹茶落季 2021-01-02 12:32

I am building a fun little app to determine if I should bike to work.

I would like to test to see if it is either Raining or Thunderstorm(ing).

publi         


        
相关标签:
6条回答
  • 2021-01-02 13:11

    You should be using the Flags attribute on your enum. Beyond that, you also need to test to see if a particular flag is set by:

    (currentWeather.Type & WeatherType.Thunderstorm == WeatherType.Thunderstorm)
    

    This will test if currentWeather.Type has the WeatherType.Thunderstorm flag set.

    0 讨论(0)
  • 2021-01-02 13:12

    I wouldn't limit yourself to the bit world. Enums and bitwise operators are, as you found out, not the same thing. If you want to solve this using bitwise operators, I'd stick to just them, i.e. don't bother with enums. However, I'd something like the following:

            WeatherType[] badWeatherTypes = new WeatherType[]
            {   
                WeatherType.Thunderstorm, 
                WeatherType.Raining
            };
    
            if (Array.IndexOf(badWeatherTypes, currentWeather.Type) >= 0)
            {
                            return false;
            }
    
    0 讨论(0)
  • 2021-01-02 13:15

    use the FlagsAttribute. That will allow you to use the enum as a bit mask.

    0 讨论(0)
  • 2021-01-02 13:18

    I'm not sure that it should be a flag - I think that you should have an range input for:

    • Temperature
    • How much it's raining
    • Wind strength
    • any other input you fancy (e.g. thunderstorm)

    you can then use an algorithm to determine if the conditions are sufficiently good.

    I think you should also have an input for how likely the weather is to remain the same for cycling home. The criteria may be different - you can shower and change more easliy when you get home.

    If you really want to make it interesting, collect the input data from a weather service api, and evaulate the decision each day - Yes, I should have cycled, or no, it was a mistake. Then perhaps you can have the app learn to make better decisions.

    Next step is to "socialize" your decision, and see whether other people hear you are making the same decisions.

    0 讨论(0)
  • 2021-01-02 13:19

    Your current code will say whether it's exactly "raining and thundery". To find out whether it's "raining and thundery and possibly something else" you need:

    if ((currentWeather.Type & _badWeatherTypes) == _badWeatherTypes)
    

    To find out whether it's "raining or thundery, and possibly something else" you need:

    if ((currentWeather.Type & _badWeatherTypes) != 0)
    

    EDIT (for completeness):

    It would be good to use the FlagsAttribute, i.e. decorate the type with [Flags]. This is not necessary for the sake of this bitwise logic, but affects how ToString() behaves. The C# compiler ignores this attribute (at least at the moment; the C# 3.0 spec doesn't mention it) but it's generally a good idea for enums which are effectively flags, and it documents the intended use of the type. At the same time, the convention is that when you use flags, you pluralise the enum name - so you'd change it to WeatherTypes (because any actual value is effectively 0 or more weather types).

    It would also be worth thinking about what "Sunny" really means. It's currently got a value of 0, which means it's the absence of everything else; you couldn't have it sunny and raining at the same time (which is physically possible, of course). Please don't write code to prohibit rainbows! ;) On the other hand, if in your real use case you genuinely want a value which means "the absence of all other values" then you're fine.

    0 讨论(0)
  • 2021-01-02 13:20

    You need to use the [Flags] attribute (check here) on your enum; then you can use bitwise and to check for individual matches.

    0 讨论(0)
提交回复
热议问题