1

有没有更简洁的方法来编写这段代码。我觉得可能有,但我不知道如何。

    if (loadedFormData.Education.AwardedADegree==true)
    {
        this.radiobuttonNoAwarded.Checked = false;
        this.radiobuttonYesAwarded.Checked = true;
    }
    if (loadedFormData.Education.AwardedADegree == false)
    {
        this.radiobuttonNoAwarded.Checked = true;
        this.radiobuttonYesAwarded.Checked = false;
    }
    else // AwardedADegree is null
    {
        this.radiobuttonNoAwarded.Checked = false;
        this.radiobuttonYesAwarded.Checked = false;

    }
4

4 回答 4

11

问“什么时候应该radiobutton检查我”的问题,而不是“当我的数据为真/假/空时应该发生什么”。

this.radiobuttonNoAwarded.Checked = loadedFormData.Education.AwardedADegree == false;
this.radiobuttonYesAwarded.Checked =  loadedFormData.Education.AwardedADegree == true;
于 2012-12-07T16:13:42.430 回答
5

我假设bool?允许该值为空。

this.radiobuttonNoAwarded.Checked = AwardedADegree.HasValue && AwardedADegree.Value;
this.radiobuttonYesAwarded.Checked = AwardedADegree.HasValue && !AwardedADegree.Value;
于 2012-12-07T16:15:17.997 回答
1

尝试这个 :

 this.radiobuttonNoAwarded.Checked = (!loadedFormData.Education.AwardedADegree) ?? false;
 this.radiobuttonYesAwarded.Checked = loadedFormData.Education.AwardedADegree ?? false;
于 2012-12-07T16:24:31.353 回答
0
this.radiobuttonNoAwarded.Checked = (loadedFormData.Education.AwardedADegree ?? false);
this.radiobuttonYesAwarded.Checked = !(loadedFormData.Education.AwardedADegree ?? true);
于 2012-12-07T16:27:16.077 回答