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
The line
is interpreted by the compiler as
This, however, doesn’t make sense –
wk
is of typeDayOfWeek
, an enumeration that assigns a number (andint
, internally) to each day of the week. Boolean values are not part of the enumeration and the compiler doesn’t know how to convert yourwk
value totrue
orfalse
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 thedays
collection?This should get you the day as a string "Monday" or "Tuesday" etc:
var dayOfWeek = DateTime.Today.ToString("dddd");
Then your check can become:
fix the class and the code
or using LINQ
Nice Easy And Simple
Note:
Means
DayOfWeek
is anEnum
and you compare it withBoolean
typeResolution
Convert
Enum
tostring
and then compare it with the day