1

Is there a possibility to directly convert a value of System.DayOfWeek into a value of Microsoft.Office.Interop.Outlook.OlDaysOfWeek. Something like:

var day = DayOfWeek.Friday;
OlDaysOfWeek days = ConvertToDaysOfWeek(day);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
scher
  • 1,813
  • 2
  • 18
  • 39
  • 3
    There's no direct conversion, but you can write one, probably using a `switch` statement. – DavidG Apr 16 '19 at 10:54
  • From your links it looks like the integer values behind the enums are different. I would write my own conversion method. – Uwe Keim Apr 16 '19 at 10:55

4 Answers4

2

You can try this:

var olDay = (OlDaysOfWeek) Enum.Parse(typeof(OlDaysOfWeek), $"ol{systemDay}");
smolchanovsky
  • 1,775
  • 2
  • 15
  • 29
2

To do this without using string manipulation, here's an option:

public OlDaysOfWeek ConvertToDaysOfWeek(DayOfWeek day)
{
    return (OlDaysOfWeek)Math.Pow(2, (int)day); 
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
1

A simple switch statement would probably be most performant:

public OlDaysOfWeek ConvertToDaysOfWeek(DayOfWeek day)
{
  switch (day)
  {
    case DayOfWeek.Monday: return OlDaysOfWeek.olMonday;
    case DayOfWeek.Tuesday: return OlDaysOfWeek.olTuesday;
    case DayOfWeek.Wednesday: return OlDaysOfWeek.olWednesday;
    case DayOfWeek.Thursday: return OlDaysOfWeek.olThursday;
    case DayOfWeek.Friday: return OlDaysOfWeek.olFriday;
    case DayOfWeek.Saturday: return OlDaysOfWeek.olSaturday;
    case DayOfWeek.Sunday: return OlDaysOfWeek.olSunday;
    default: throw new ArgumentOutOfRangeException("What day is this?", "day");
  }
}

Alternatively, you could probably parse the value and return the mapped value based on the enum value name.

public OlDaysOfWeek ConvertToDaysOfWeek(DayOfWeek day)
{
  return (OlDaysOfWeek) Enum.Parse(typeof(OlDaysOfWeek), "ol" + day.ToString());
}

The OlDaysOfWeek enum utilises a power-of-2 sequence, which is typically used when combining values as bitwise flags. DaysOfWeek has a simple linear sequence reflected as 0-6 - this is why you can't compare using the backing int value

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
1

Finally I found the following solution. Thanks for the hints.

public static OlDaysOfWeek AsDaysOfWeek(this DayOfWeek dayOfWeek)
{
    return (OlDaysOfWeek)(1 << (int)dayOfWeek);
}

To be used like this:

var day = DayOfWeek.Friday;
OlDaysOfWeek days = day.AsDaysOfWeek();

Additionally these are the unit tests in NUnit for the method:

[TestCase(DayOfWeek.Monday, OlDaysOfWeek.olMonday)]
[TestCase(DayOfWeek.Tuesday, OlDaysOfWeek.olTuesday)]
[TestCase(DayOfWeek.Wednesday, OlDaysOfWeek.olWednesday)]
[TestCase(DayOfWeek.Thursday, OlDaysOfWeek.olThursday)]
[TestCase(DayOfWeek.Friday, OlDaysOfWeek.olFriday)]
[TestCase(DayOfWeek.Saturday, OlDaysOfWeek.olSaturday)]
[TestCase(DayOfWeek.Sunday, OlDaysOfWeek.olSunday)]
public void AsDaysOfWeek(DayOfWeek dayOfWeek, OlDaysOfWeek expectedResult)
{
    var result = dayOfWeek.AsDaysOfWeek();

    Assert.That(result, Is.EqualTo(expectedResult));
}
scher
  • 1,813
  • 2
  • 18
  • 39
  • Did you find this answer online? – DavidG Apr 16 '19 at 11:14
  • Note that you will have problems if any of the enumeration options change value. – smolchanovsky Apr 16 '19 at 11:16
  • "Additionally these are the unit tests" - you forgot the tests for out-of-range enum values :) – Joe Apr 16 '19 at 11:22
  • @StanislavMolchanovsky If microsoft changes the enumeration values, the most likely result is that *every* program using Outlook interop will have problems. That would be a public interface change. That's not something MS does lightly. – Bradley Uffner Apr 16 '19 at 11:23
  • @BradleyUffner you are right, it is unlikely. But when choosing a solution it is necessary to consider all its pros and cons :) – smolchanovsky Apr 16 '19 at 11:38