Reputation: 21206
How can I cast the return enum value to its integer value?
I want to get all flags which are set in the _allVisibleDays
enum as integer values just like:
Tuesday
and Wednesday
are set then I want to cast these names into a DayOfWeek
enum to get the integer values:
So Tuesday
returns me 2
and Wednesday
3
instead of 2
and 4
of the below enum.
I have found this method on SO:
public IEnumerable<int> GetFlags()
{
foreach (Enum value in Enum.GetValues(typeof(VisibleDayOfWeek)))
if (_allVisibleDays.HasFlag(value))
yield return (int) value;
}
but I can ot make it work just for the simple example of casting an enum (value above) to the integer...
private VisibleDayOfWeek _allVisibleDays;
[Flags]
enum VisibleDayOfWeek : int
{
None = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64
}
Upvotes: 3
Views: 1650
Reputation: 69362
You can loop through the Enum
array and check the index.
public int GetEnumIndex(VisibleDayOfWeek day, bool ToBeConvertedToDoW)
{
var allValues = Enum.GetValues(typeof(VisibleDayOfWeek));
for (int i = 0; i < allValues.Length ; i++)
{
if ((VisibleDayOfWeek)allValues.GetValue(i) == day)
{
//if you plan to convert to a DayOfWeek, this will return the
//index for the DoW enum
if(ToBeConvertedToDoW)
return (i % 7);
else //else return the actual index from VisibleDayOfWeek enum
return i;
}
}
return -1;
}
Upvotes: 1
Reputation: 23198
Since your VisibleDayOfWeek
is a Flags
enumeration, and DayOfWeek is not, there is no direct corresponding translation between the two (for example, VisibleDayOfWeek.Friday
is 16, while DayOfWeek.Friday
is 5). Especially as these are just days of the week and probably won't change in the future (I hope!) I think the best bet here is just write a simple utility method with checks:
public IEnumerable<DayOfWeek> ConvertFromVisible(VisibleDayOfWeek visibleDay)
{
if ((visibleDay & VisibleDayOfWeek.Monday) == VisibleDayOfWeek.Monday)
yield return DayOfWeek.Monday;
if ((visibleDay & VisibleDayOfWeek.Tuesday) == VisibleDayOfWeek.Tuesday)
yield return DayOfWeek.Tuesday;
if ((visibleDay & VisibleDayOfWeek.Wednesday) == VisibleDayOfWeek.Wednesday)
yield return DayOfWeek.Wednesday;
if ((visibleDay & VisibleDayOfWeek.Thursday) == VisibleDayOfWeek.Thursday)
yield return DayOfWeek.Thursday;
if ((visibleDay & VisibleDayOfWeek.Friday) == VisibleDayOfWeek.Friday)
yield return DayOfWeek.Friday;
if ((visibleDay & VisibleDayOfWeek.Saturday) == VisibleDayOfWeek.Saturday)
yield return DayOfWeek.Saturday;
if ((visibleDay & VisibleDayOfWeek.Sunday) == VisibleDayOfWeek.Sunday)
yield return DayOfWeek.Sunday;
}
This has the benefit of automatically returning the DayOfWeek
enumeration rather than the integers to cast later as well.
In addition, since this skips reflection, it should be more performant as well as more resistant to value changes (though I doubt this will happen anyway).
EDIT: If you want to tweak the order they're returned (say you want to start from Sunday and go to Saturday) you can just alter the order of the lines above easily.
Upvotes: 2