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「データがtrue/false / nullの場合に何が起こるか」ではなく、「いつチェックする必要があるか」という質問をします。

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

bool?値をnullにできると仮定します。

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 に答える