0

在我使用“私人”或“公共”的几乎所有情况下,我都会遇到这个错误。我最近将我的课程改为 get; set;从...

public string Title
{
    get { return title; }
    set { title = value; }

}

至...

public static string Title
{
    get;
    set;
}

有错误出现在...

private void cBxEmployment_SelectedIndexChanged_1(object sender, EventArgs e)
{
    //sets the job list combo box to visible if "Employed" or "Self Employed" is selected by user, if not selected jobs list combo box is not visible.
    if (cBxEmployment.SelectedIndex == 0 || cBxEmployment.SelectedIndex == 1)
    {
        lblJob.Visible = true;
        cBxJob.Visible = true;
    }
    else
    {
        lblJob.Visible = false;
        cBxJob.Visible = false;
    }
}

private void btnExit_Click(object sender, EventArgs e)
{
    //when exit button is clicked user will get a message box asking if they want to exit, if yes program closes
    DialogResult answer = MessageBox.Show("Would you like to exit?", "Exit", MessageBoxButtons.YesNo);
    if (answer == DialogResult.Yes)
    {
        //close program
        Application.Exit();
    }
}

private void btnClear_Click(object sender, EventArgs e)
{
    cBxTitle.SelectedIndex = -1;
    txtFName.Text = "";
    txtLastname.Text = "";
    txtDOB.ResetText();
    cBxEmployment.SelectedIndex = -1;
    cBxJob.SelectedIndex = -1;

    cBxRelationship.SelectedIndex = -1;

    cBxTitle.Focus();

等等...

即使我将其从公共更改为私人,反之亦然,它仍然会引发此错误。

4

2 回答 2

1

您有一个嵌套函数,在 C# 中,这些函数称为局部函数,没有作用域。所以你需要删除访问修饰符。

来自微软文档

局部函数不能包含静态修饰符。包含static关键字生成编译器错误 CS0106,“修饰符 'static' 对此项无效

于 2019-12-30T19:58:03.393 回答
1

您还在static示例中将属性更改为 like:public static string Title

它应该是

public string Title
{
    get;
    set;
}
于 2019-12-30T19:58:10.203 回答