-1

I'm using EWSoftware.PDI library to build a recurring dates list.

var r = new Recurrence();
r.RecurWeekly(2, DaysOfWeek.Monday | DaysOfWeek.Tuesday);

DaysOfWeek is an enum type.

I'm looking to create the second parameter of RecurWeekly dynamically. It expects the parameter to be of type (enum) EWSoftware.PDI.DaysOfWeek.

Is it possible to create an array that can be passed through as the second parameter, or an array that is exploded in to the correct parameters in the separated list?

Background:

I'm building a calendar with recurring events. A list of selected days is passed through to a method containing the code above. Depending on the days selected, it will build a variable that I can pass through to the RecurWeekly function.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dion
  • 136
  • 1
  • 2
  • 15
  • 2
    Are you looking for [`params`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params)? – Uwe Keim Dec 03 '20 at 14:11
  • 2
    Do you know about the [`|=` operator](https://stackoverflow.com/questions/6942477/what-does-single-pipe-equal-and-single-ampersand-equal-mean)? – gunr2171 Dec 03 '20 at 14:12
  • 1
    Related: https://stackoverflow.com/questions/1030090/how-do-you-pass-multiple-enum-values-in-c – xdtTransform Dec 03 '20 at 14:23
  • If you have the DaysOfWeek value as ID you can simply sum the selected items and pass it to the function – Drag and Drop Dec 03 '20 at 14:40
  • Did either of the two answers you got answer your question (they are both good answers)? You can accept one of them, and you can upvote one or both of them (the latter, if both are useful). – Flydog57 Dec 04 '20 at 17:57

2 Answers2

1

Assuming that your enum looks something like this:

[Flags]
public enum DaysOfWeek
{
    Sunday = 0x01,
    Monday = 0x02,
    Tuesday = 0x04,
    Wednesday = 0x08,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

And you have some code that can create a string that looks like (say, the result of checking which checkboxes are checked):

var days = "Sunday,Monday,Friday";

Then you can do something like this:

var success = Enum.TryParse<DaysOfWeek>(days, out var daysOfWeek);

The result of that is that daysOfWeek will look like this in the debugger:

daysOfWeek:  Sunday|Monday|Friday   (Type=TestConsole.Program.DaysOfWeek)
Flydog57
  • 6,851
  • 2
  • 17
  • 18
0

Given an array of flag enumeration values like this:

var arr = new DaysOfWeek[] { DaysOfWeek.Monday, DaysOfWeek.Wednesday, DaysOfWeek.Friday };

...you can use the following method to convert the array into a single DaysOfWeek:

static DaysOfWeek Convert(DaysOfWeek[] days)
{
    DaysOfWeek value = default;
    foreach (DaysOfWeek day in days)
        value |= day;
    return value;
}

...that the method accepts:

r.RecurWeekly(arr.Length, Convert(arr));
mm8
  • 163,881
  • 10
  • 57
  • 88