我如何解决预期的类,委托,枚举,接口,结构编译错误?

时间:2015-07-01 16:01:53

标签: c#

我在私人虚空中收到此错误...

这是一个真正的简单程序,我想使用字符串变量显示消息。

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        {
            string greetme;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            greetme = textbox1.text;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello {0}", greetme);
        } 
    }
}

2 个答案:

答案 0 :(得分:3)

只需删除greetme声明周围的括号:

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

    string greetme;

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        greetme = textbox1.text;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hello {0}", greetme);
    } 
}

答案 1 :(得分:0)

这些错误几乎总是意味着您不应该使用括号。在这种情况下,它是“Public Form1()”方法之后的括号/代码。没有方法声明,也没有括号。