将Int32转换为Bool

时间:2016-12-13 15:55:20

标签: c#

我目前正在编写一个查看SQL代理作业的应用程序。我正在尝试将FrequencyInterva l属性转换为7个bool字段(每天一个)

我在考虑为每种可能的组合使用案例陈述,但是想知道是否有更有效的方法?

1 个答案:

答案 0 :(得分:9)

您不需要七个bool变量,但您可以使用attribute Flags定义枚举,其值等于为该属性定义的值。 然后将FrequencyInterval属性分配给此枚举类型的变量。

[Flags]
public enum frequencyDaysEnum
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64,
// You can also add these but they are not necessary
//    WeekDays = 62,
//    WeekEnds = 65,
//    EveryDay = 127
}

void Main()
{
    int value = 65; // <= this is the value of your property
    frequencyDaysEnum days = (frequencyDaysEnum)value;

    if(days.HasFlag(frequencyDaysEnum.Saturday))
        Console.WriteLine("It has frequency on Saturday");

    if (days.HasFlag(frequencyDaysEnum.Sunday))
        Console.WriteLine("It has frequency on Sunday");
}