skip to Main Content

I have a website that has checkboxes that read Monday – Friday and the user is able to select none, one or more than one.

CDay list contains the days of the week: Monday – Friday. What I want to do is take in the current day of the week and return true if the user has chosen that day

list code:

 public List<CDay> days { get; set; }
    public CDay()
    {
     Name = string.Empty; // Monday, Tuesday, Wednesday, Thursday, Friday
     Selected = false; //will be true if user has selected this checkbox
    }
    

my logic:
// is the current day the same for any of the days the user has chosen

private bool currentDayOfTheWeek (DateTime day, List <CDay> days)
        {
          System.DayOfWeek wk = DateTime.Today.DayOfWeek;
        
          for (int i = 0; i < days.Count; i++)
          {

            if ( days[i].Selected.Equals(true) && wk) 
                 {
                     return true;
                 }
              }
            return false;
          }

My problem is that this statement: days[i].Selected.Equals(true) && wk gives me an "Operator ‘==’ cannot be applied to operands of type ‘bool’ and ‘DayOfWeek’.. not sure how I would go about this error?

4

Answers


  1. The line

    if ( days[i].Selected.Equals(true) && wk)
    

    is interpreted by the compiler as

    if ( days[i].Selected.Equals(true) && wk == true)
    

    This, however, doesn’t make sense – wk is of type DayOfWeek, an enumeration that assigns a number (and int, internally) to each day of the week. Boolean values are not part of the enumeration and the compiler doesn’t know how to convert your wk value to true or false so it throws that error.

    To fix this, you need to explicitly specify what condition wk should pass – reading your code, it could perhaps equal to the selected day in the days collection?

    Login or Signup to reply.
  2. This should get you the day as a string "Monday" or "Tuesday" etc:

    var dayOfWeek = DateTime.Today.ToString("dddd");

    Then your check can become:

    for (int i = 0; i < days.Count; i++)
    {
        if (days[i].Selected.Equals(true) && dayOfWeek == days[i].Name) 
        {
            return true;
        }
    }
    return false;
    
    Login or Signup to reply.
  3. fix the class and the code

    private bool currentDayOfTheWeek(DateTime day, List<CDay> days)
    {
        var dow = day.DayOfWeek.ToString();
    
        foreach (var d in days)
            if (d.Selected && dow == d.Name) return true;
    
        return false;
    }
    
    public class CDay
    {
        public string Name {get; set;} = string.Empty; // Monday, Tuesday, Wednesday, Thursday, Friday
        public bool Selected {get; set;} // false by default, will be true if user has selected this checkbox
    }
    

    or using LINQ

    private bool currentDayOfTheWeek(DateTime day, List<CDay> days)
    {
        var dow = day.DayOfWeek.ToString();
    
        return days.Any(d => d.Selected && dow == d.Name);
    }
    
    
    Login or Signup to reply.
  4. Nice Easy And Simple

    private bool currentDayOfTheWeek (DateTime day, List <CDay> days)
    {
        System.DayOfWeek wk = DateTime.Today.DayOfWeek;
        foreach (var item in days)
        {
            if (item.Selected && item.Name == wk.ToString()) 
            {
                return true;
            }
        }
        return false;
    }
    

    Note:

    Operator ‘==’ cannot be applied to operands of type ‘bool’ and
    ‘DayOfWeek

    Means

    DayOfWeek is an Enum and you compare it with Boolean type

    Resolution

    Convert Enum to string and then compare it with the day

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search