简体   繁体   中英

There is no implicit conversion between bool and System.DayOfWeek

I want to add into a List days if checkboxes are checked so i do the following:

List<DayOfWeek> result = rbDaily.Checked || rbTwoWeeks.Checked 
              ? new List<DayOfWeek>
              { 
                  checkboxMon.Checked ? true : DayOfWeek.Monday, checkboxWed.Checked ? true : DayOfWeek.Wednesday
              }
              : null;

somehow it says for those lines:

checkboxMon.Checked ? true : DayOfWeek.Monday
checkboxWed.Checked ? true : DayOfWeek.Wednesday

There is no implicit conversion between bool and System.DayOfWeek. What should i put there additionally to say not add that day if false is returned?

There is no way of achieving what you want using a collection initializer.

Split your code up a bit:

List<DayOfWeek> result = null;
if (rbDaily.Checked || rbTwoWeeks.Checked)
{
    result = new List<DayOfWeek>();
    if (checkboxMon.Checked)
        result.Add(DayOfWeek.Monday);
    if (checkboxWed.Checked)
        result.Add(DayOfWeek.Wednesday);
}

If you have lots of different checkboxes for different days of the week, you can do something like:

var checkboxes = new List<(Checkbox checkbox, DayOfWeek dow)>()
{
    (checkboxMon, DayOfWeek.Monday),
    ... etc ...
};

var result = (rbDaily.Checked || rbTwoWeeks.Checked)
    ? checkboxes.Where(x => x.checkbox.Checked).Select(x => x.dow).ToList()
    : null;

According to conditional operator

The type of consequent and alternative must be the same, or there must be an implicit conversion from one type to the other

You should return the same type for both expressions. You can declare your list as List<DayOfWeek?> for example and initialize like that

? new List<DayOfWeek?>
              { 
                  checkboxMon.Checked ? DayOfWeek.Monday : (DayOfWeek?)null, checkboxWed.Checked ? DayOfWeek.Wednesday : (DayOfWeek?)null
              }
              : null;

Or even get rid of the conditional operator and use the code like that

if (rbDaily.Checked || rbTwoWeeks.Checked)
{
    var list = new List<DayOfWeek>();
    if (checkboxMon.Checked)
        list.Add(DayOfWeek.Monday);
    if (checkboxWed.Checked)
        list.Add(DayOfWeek.Wednesday);
}

You're trying to store boolean values in a List<DayOfWeek> . (As well as returning an indeterminate type from a ternary conditional expression.)

Consider the semantics of your description:

I want to add into a List days if checkboxes are checked

You're describing exactly the code you want. Express it as pseudo-code:

If checkbox is checked {
    Add into a list
}

So create your collection, and conditionally add your elements to it. For example:

List<DayOfWeek> result = new List<DayOfWeek>();
if (checkboxMon.Checked)
{
    result.Add(DayOfWeek.Monday);
}
if (checkboxWed.Checked)
{
    result.Add(DayOfWeek.Wednesday);
}
// etc.

The conditional operator takes a boolean expression and yields two possible outputs that must be of the same type. It can't be used to return a value or "nothing" if the value is false. You want if statements instead:

List<DayOfWeek> userType = null;

if (rbDaily.Checked || cbTwoWeeks.Checked)
{
    userType = new List<DayOfWeek>();
    if (checkboxMon.Checked)
    { 
        userType.Add(DayOfWeek.Monday);
    }
    if(checkboxWed.Checked)
    { 
        userType.Add(DayOfWeek.Wednesday);
    }
}

You could do slick things like put the checkboxes in a collection and project to a list using Where , etc. but using if is probably the cleanest (meaning easiest to read) method.

checkboxMon.Checked ? true : DayOfWeek.Monday

This not make any sense. true is a bool type and DayOfWeek.Monday is a Date type. Just rewrite your code as

if(checkboxMon.Checked)
 new List<DayOfWeek>{DayOfWeek.Monday };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM