-1

I have one enum and one function like this

        enum DaysOfWeek
        {
           Sunday = 1,
           Monday = 2,
           Tuesday = 4,
           Wednesday = 8,
           Thursday = 16,
           Friday = 32,
           Saturday = 64
        }

        public void RunOnDays(DaysOfWeek days)
        {
           // Do your work here..
        }

    // Im Calling The Function like this and passing the parameters with Pipe Seprated
       RunOnDays(DaysOfWeek.Tuesday | DaysOfWeek.Thursday);

Now Scenario is In MY UI I have some checkboxes like Monday to sunday and user can select all days or can select at least one. And I want to pass the selected values to my function RunOnDays. So it can be single value or many values. How I can pass values dynamically to that method on the selection of user.

Arslan Pervaiz
  • 1,575
  • 4
  • 29
  • 62
  • 5
    mark your enum with [Flags] attribute – Giedrius Jun 04 '13 at 12:24
  • There's already a [`DayOfWeek`](http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx) enum, why do you reinvent the wheel? – Tim Schmelter Jun 04 '13 at 12:26
  • 1
    And you've shown how you're passing a "multiple value" value to the method already... so what's the question? – Jon Skeet Jun 04 '13 at 12:27
  • 1
    @TimSchmelter Well I guess the existing one is not [Flags] so you can't combine the days. – Matthew Watson Jun 04 '13 at 12:31
  • @ArslanPervaiz What issue are you having with what you already have? Are you asking how you generate the flags from the checkbox settings? – Matthew Watson Jun 04 '13 at 12:32
  • 1
    @MatthewWatson: you're right, i see where OP's code is from: http://stackoverflow.com/a/1030091/284240 – Tim Schmelter Jun 04 '13 at 12:36
  • OK Let Me Tell You. I have Some DLL and in that DLL that method and enum is defined. I donot have rights to change the enum to flags or change the method. In my UI I have Some Checkboxes thats have some values like. Sunday, Monday, Saturday, Friday etc. So Which Checkbox will be checked I have to pass that as parameter seprated with | as I mentioned in my Question. – Arslan Pervaiz Jun 04 '13 at 12:38
  • @TimSchmelter: Yes This Code is Just For Some Generic Example. As I Told My In Above Comment I Have That Scenario. – Arslan Pervaiz Jun 04 '13 at 12:40

3 Answers3

5

Use [Flags] attribute on your enum

[Flags]
enum DaysOfWeek
{
   Sunday = 1,
   Monday = 2,
   Tuesday = 4,
   Wednesday = 8,
   Thursday = 16,
   Friday = 32,
   Saturday = 64
}

public void RunOnDays(DaysOfWeek days)
{
           // Do your work here..
}
Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
  • 1
    The `Flags` attribute should be applied to the enum, not the parameter. – Jon Skeet Jun 04 '13 at 12:26
  • 1
    Okay. Now while this is better, it's not actually *necessary* - just just changes what `ToString` and `Parse` do, I believe. The OP already seems to know how to create a compound value using `|`, so it's not actually clear what the question is about... but I suspect that this doesn't solve it. – Jon Skeet Jun 04 '13 at 12:29
3

It is easier to pass them through a List<T> or IEnumerable<T>.

public void RunOnDays(IEnumerable<DaysOfWeek> days)
{
    // do something amazing
}

public void DoWork()
{
    var days = new List<DaysOfWeek>();

    // put each checked day into days

    RunOnDays(days);
}

EDIT: If I understand your post correctly, you are asking how to dynamically apply the | operator to an indeterminate list of enums, correct? If RunOnDays is defined in an unmodifiable DLL, you first need to know if it supports compounded enums. If so, you can still use the IEnumerable approach and combine through iteration.

DaysOfWeek checkedDays;

foreach (var day in days)
{
    checkedDays |= day;
}

RunOnDays(checkedDays);
Jordan Parmer
  • 36,042
  • 30
  • 97
  • 119
  • I have Some DLL and in that DLL that method and enum is defined. I donot have rights to change the enum to flags or change the method. In my UI I have Some Checkboxes thats have some values like. Sunday, Monday, Saturday, Friday etc. So Which Checkbox will be checked I have to pass that as parameter seprated with | as I mentioned in my Question. – Arslan Pervaiz Jun 04 '13 at 12:43
  • I'm not sure what you are asking. Is `RunOnDays` your method or one defined in the library? Which part of your post are you having problems with? – Jordan Parmer Jun 04 '13 at 12:47
  • Then Please Read My Question First. – Arslan Pervaiz Jun 04 '13 at 12:50
1

Ok, I'm not entirely sure what you need to know, but I'm going to have a guess.

Assume you have one checkbox per day, called mondayCheckbox, tuesdayCheckbox and so on.

Now you want to get a single int value that represents which of those checkboxes are selected.

You can do that as follows:

DaysOfWeek days = 0;

if (mondayCheckbox.Checked)
    days |= DaysOfWeek.Monday;

if (tuesdayCheckbox.Checked)
    days |= DaysOfWeek.Tuesday;

... And so on up to:

if (sundayCheckbox.Checked)
    days | = DaysOfWeek.Sunday;

if (days != 0)
{
    RunOnDays(days);
}
else
{
    // Handle no days selected.
}

I think you should also add a None to your DaysOfWeek enum:

[Flags]
enum DaysOfWeek
{
   None   = 0,
   Sunday = 1,
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276