1

I have the need to create scheduled windows tasks using a C# app. I have a comma separated string that stores the months I'd like to run the task on. The string contains the short values for the type MonthsOfYear - eg. "1,2,4,16,128,1024".

The example I have shows that you can assign multiple months seperated by a pipe as follows:

MonthlyTrigger mt = new MonthlyTrigger();
mt.StartBoundary = Convert.ToDateTime(task.getStartDateTime());
mt.DaysOfMonth = new int[] { 10, 20 };
mt.MonthsOfYear = MonthsOfTheYear.July | MonthsOfTheYear.November;

My question is, how do I assign multiple months to the trigger dynamically, using the values from the comma seperated string.

Denis Wasilew
  • 503
  • 3
  • 9
  • 29
ITGuy
  • 13
  • 2

1 Answers1

2

I'm not quite sure, what your problem is. And you didn't post code of your Trigger or your enum. Because of this i'll provide a complete example with a List for comparesion:

public class MonthlyTrigger
{
    [Flags] // Important because we want to set multiple values to this type
    public enum MonthOfYear
    {
        Jan = 1, // 1st bit
        Feb = 2, // 2nd bit..
        Mar = 4,
        Apr = 8,
        May = 16,
        Jun = 32,
        Jul = 64,
        Aug = 128,
        Sep = 256,
        Oct = 512,
        Nov = 1024,
        Dec = 2048
    }

    public HashSet<int> Months { get; set; } = new HashSet<int>(); // classical list to check months
    public MonthOfYear MonthFlag { get; set; } // out new type
}

public static void Main(string[] args)
{
    MonthlyTrigger mt = new MonthlyTrigger();

    string monthsFromFileOrSomething = "1,3,5,7,9,11"; // fake some string..

    IEnumerable<int> splittedMonths = monthsFromFileOrSomething.Split(',').Select(s => Convert.ToInt32(s)); // split to values and convert to integers

    foreach (int month in splittedMonths)
    {
        mt.Months.Add(month); // adding to list (hashset)

        // Here we "add" another month to our Month-Flag => "Flag = Flag | Month"
        MonthlyTrigger.MonthOfYear m = (MonthlyTrigger.MonthOfYear)Convert.ToInt32(Math.Pow(2, month - 1));
        mt.MonthFlag |= m;
    }

    Console.WriteLine(String.Join(", ", mt.Months)); // let's see our list
    Console.WriteLine(mt.MonthFlag); // what is contained in our flag?
    Console.WriteLine(Convert.ToString((int)mt.MonthFlag, 2)); // how is it binarily-stored?


    // Or if you like it in one row:
    mt.MonthFlag = 0;
    foreach (MonthlyTrigger.MonthOfYear m in monthsFromFileOrSomething.Split(',').Select(s => (MonthlyTrigger.MonthOfYear)Convert.ToInt32(s)))
        mt.MonthFlag = m;

    return;
}

Adding or removing single Flags in an enum:

MyEnumType myEnum = 1; // enum with first flag set

myEnum |= 2; // Adding the second bit. Ofcouse you can use the enum-name here "MyEnumType.MyValueForBitTwo"
// Becuase:
//   0000 0001
// | 0000 0010   "or"
// = 0000 0011

myEnum &= (int.MaxValue - 2) // Deletes the second enum-bit.
// Because:
//   0000 0011
// & 1111 1101   "and"
// = 0000 0001
kara
  • 3,205
  • 4
  • 20
  • 34
  • Apologies, I only realized now that I didn't provide alot of info. My problem is assigning multiple months to the trigger from a css. A user is given the option in another UI to select different months to schedule a task to be run on. Those months are stored as Int in a comma seperated string which will be retrieved before creating the task. So while I know how to assign multiple months if I were to reference the months directly (mt.MonthsOfYear = MonthsOfTheYear.July | MonthsOfTheYear.November), I do not know how to do this using a css. It must to trigger for each month chosen by the user. – ITGuy Mar 11 '19 at 09:33
  • With a few tweaks, I got it working. Thanks so much! – ITGuy Mar 11 '19 at 09:48