First of all perhaps this is a problem due to a bad design. Here is my scenario simplified:
I have a class call Day it represent a day of the year, with its date and the "type" of day.
public class Day{
Date Key;
TypeDay type;
}
So the day can be a worked day or a holiday one:
public enum TypeDay{
Work,
Holiday
}
So far so good, but now holiday days can have subtypes like paidDays and NonPaid days I would need another enum to represent that subtypes or add all subtypes in a single enum (in this example i have 2 days and 2 subtypes but in real live i have 50-40 types) so this get so messy .
public enum TypeDay{
Work,
Holiday_NonPaid
Holiday_Paid
}
How can i build this in a better way,any ideas?
CodePudding user response:
Enumerators didn't support that behaviour, If you feel that a structure like that is needed you can 'simulate' it with a static class
static class TypeDay
{
public const int Work = 0;
public static class Holiday
{
public const int Paid = 1;
public const int NonPaid = 2;
}
}
CodePudding user response:
You could use flags to treat 'paid' and 'holiday' as independent properties of the enum:
[Flags]
public enum TypeDay{
Na = 0,
Work = 1,
Holiday = 2,
Holiday_NonPaid = Holiday,
Holiday_Paid = Holiday | Work
}
This assumes a workday is paid. This lets you either check the exact type, or if the day has a specific flag, isHoliday = type.HasFlag(TypeDay.Holiday)
or isPaid = type.HasFlag(TypeDay.Work)
.
But there are other approaches, like using classes instead of enums.The most appropriate approach will depend on the exact use case and what you want to do with the values.