-2

iam getting from EWS an WeeklyPattern with DaysOfTheWeek. It's in an Array an can be one day or Multiple.

I have an Enum which is called DayOfWeekMask and i need to convert the Array to the Enum.

When the Weeklypatter contains "Monday,Tuesday"

The function should return:

return DayOfWeekMask.olMonday | DayOfWeekMask.olThursday ;

when the weeklypatter contains 3 days it should return 3 elements.

is this possible?

Eray Geveci
  • 1,099
  • 4
  • 17
  • 39
  • 1
    does `WeeklyPattern` contain 1 string delimited by `,` or a list of strings? – stefankmitph Apr 24 '15 at 13:41
  • possible duplicate of [How do I Convert a string to an enum in C#?](http://stackoverflow.com/questions/16100/how-do-i-convert-a-string-to-an-enum-in-c) – BCdotWEB Apr 24 '15 at 13:42
  • 4
    @BCdotWEB I don't think this is a duplicate. They are two different questions. – Soner Gönül Apr 24 '15 at 13:43
  • Potential duplicate of [What does the \[Flags\] Enum Attribute mean in C#?](http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c). more Reading [here](https://msdn.microsoft.com/en-us/library/System.FlagsAttribute%28v=vs.110%29.aspx) – Bernd Linde Apr 24 '15 at 13:46
  • @BerndLinde That is not a duplicate. That explains the flags attribute for an enum. This is asking about converting a list of enum names into a single value of an enum. – juharr Apr 24 '15 at 13:49
  • @juharr noticed that as well after I submitted the flag (But still a good read for this case). Waiting on the OP to say what the array is to write up an answer – Bernd Linde Apr 24 '15 at 13:50
  • [Recurrence.WeeklyPattern.DaysOfTheWeek](https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.recurrence.weeklypattern.daysoftheweek(v=exchg.80).aspx) is a [DayOfTheWeekCollection](https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.dayoftheweekcollection(v=exchg.80).aspx). This collection contains [DayOfTheWeek](https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.dayoftheweek(v=exchg.80).aspx) enum items. So most likely they can be converted to your custom `DayOfWeekMask` enum. – BCdotWEB Apr 24 '15 at 13:52
  • I understand that WeeklyPattern.DaysOfTheWeek( cf. https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.recurrence.weeklypattern.daysoftheweek%28v=exchg.80%29.aspx) is not an array of strings but a collection of enums. – Peter - Reinstate Monica Apr 24 '15 at 13:53
  • 1
    @GrantWinney is it this: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.recurrencepattern.dayofweekmask.aspx? – Peter - Reinstate Monica Apr 24 '15 at 13:55
  • 2
    Eray, read the documentation properly, write sample code and ask again. With a minimal self contained example. Obtaining the necessary information is the first step in programming. – Peter - Reinstate Monica Apr 24 '15 at 13:57

3 Answers3

1

Assuming you have a string formatted as a comma-separated list of days:

"Monday, Tuesday, Thursday"

You can Split it into a string array and parse each element:

var days = weekString.Split(',', StringSplitOptions.RemoveEmptyEntries);

DayOfWeekMask? result = null;

foreach (var day in days) {
    var enumDayName = "ol" + day;
    if (Enum.IsDefined(typeof(DayOfWeekMask), enumDayName)) {
        result |= (DayOfWeekMask)Enum.Parse(typeof(DayOfWeekMask), enumDayName, true);
    }
}

Note that you need to define values for each element of DayOfWeekMask to make the ORing work properly!

If your input is a DayOfTheWeekCollection, then you can just directly convert the Enum values:

DayOfTheWeekCollection days;  // get this from somewhere

foreach (var day in days) {
    switch (day) {
        case DayOfTheWeek.Monday:
            result |= DayOfWeekMask.Monday;
            break;
        case DayOfTheWeek.Tuesday:
            // etc.
    }
}
Brian Kintz
  • 1,983
  • 15
  • 19
1

The names in both enums are very similar (OlDaysOfWeek just appends "ol" to the beginning of the name), so we can take advantage of that.

This converts the DaysOfTheWeek property (a collection of DayOfTheWeek values) to OlDaysOfWeek (the type on the DayOfWeekMask property), which seems to be what you're asking for:

OlDaysOfWeek daysOfTheWeek =
    weeklyPattern.DaysOfTheWeek
                 .Select(x => (OlDaysOfWeek) Enum.Parse(typeof (OlDaysOfWeek), "ol" + x))
                 .Aggregate((x, y) => x | y);

If your initial collection includes DayOfTheWeek.Tuesday and DayOfTheWeek.Thursday, this will output a value equivalent to OlDaysOfWeek.olTuesday | OlDaysOfWeek.olThursday.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

I don't know the exact format of your variables, but something along these lines:

return (WeeklyPattern.Contains( "Monday") ? DayOfWeekMask.olMonday : 0)
  | (WeeklyPattern.Contains( "Tuesday") ? DayOfWeekMask.olTuesday : 0)
  | (WeeklyPattern.Contains( "Wednesday") ? DayOfWeekMask.olWednesday : 0)
  | (WeeklyPattern.Contains( "Thursday") ? DayOfWeekMask.olThursday : 0)
  | (WeeklyPattern.Contains( "Friday") ? DayOfWeekMask.olFriday : 0);
Graham
  • 799
  • 2
  • 5
  • 14