winforms 为什么字段不返回任何内容?[closed]

n7taea2i  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(122)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
三个月前关门了。
Improve this question
我有一个表单,我使用复选框在单击时分配字符串。但是,我注意到我有一个错误:“该字段被赋值,但其值从未被使用”。我一开始忽略了它,但当我运行代码时,它什么也不返回,即使后来赋值了一个字符串。

public partial class FindQuestionMenu : Form
{
    private string selectedMode;
    private string selectedTopic;
    public FindQuestionMenu()
    {
        InitializeComponent();
    }
    private void PPQModeSelect_CheckedChanged(object sender, EventArgs e)
    {
        selectedMode = "Past Paper Questions Mode";
    }
    private void RandomQuestionSelected_CheckedChanged(object sender, EventArgs e)
    {
        selectedMode = "Random Question Generator Mode";
    }
    private void SineCosineRuleOption_CheckedChanged(object sender, EventArgs e)
    {
        selectedTopic = "Sine and Cosine Rule";
        Close();
    }
    public string SelectedMode { get; set; }
    public string SelectedTopic { get; set; }
}
kse8i1jr

kse8i1jr1#

请注意,该 * 字段 *

private string selectedMode

与 * 属性**没有 * 共同之处

public string SelectedMode { get; set; }

看起来,您根本不需要 * 显式支持字段 *,您可以为 * 属性 * 赋值:

public partial class FindQuestionMenu : Form
{
    public FindQuestionMenu()
    {
        InitializeComponent();
    }

    private void PPQModeSelect_CheckedChanged(object sender, EventArgs e)
    {
        SelectedMode = "Past Paper Questions Mode";
    }

    private void RandomQuestionSelected_CheckedChanged(object sender, EventArgs e)
    {
        SelectedMode = "Random Question Generator Mode";
    }

    private void SineCosineRuleOption_CheckedChanged(object sender, EventArgs e)
    {
        SelectedTopic = "Sine and Cosine Rule";
        Close();
    }

    // You may want to declare "private set" not just "set"
    public string SelectedMode { get; set; }
    // You may want to declare "private set" not just "set"
    public string SelectedTopic { get; set; }
}

相关问题