无法分配,因为它是方法组C#?

时间:2013-11-04 16:39:56

标签: c# .net methods assign method-group

无法分配“AppendText”,因为它是“方法组”。

public partial class Form1 : Form
{
    String text = "";

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String inches = textBox1.Text;
        text = ConvertToFeet(inches) + ConvertToYards(inches);
        textBox2.AppendText = text;
    }

    private String ConvertToFeet(String inches)
    {
        int feet = Convert.ToInt32(inches) / 12;
        int leftoverInches = Convert.ToInt32(inches) % 12;
        return (feet + " feet and " + leftoverInches + " inches." + " \n");
    }

    private String ConvertToYards(String inches)
    {
        int yards = Convert.ToInt32(inches) / 36;
        int feet = (Convert.ToInt32(inches) - yards * 36) / 12;
        int leftoverInches = Convert.ToInt32(inches) % 12;
        return (yards + " yards and " + feet + " feet, and " + leftoverInches + " inches.");
    }
}

错误位于button1_Click方法内的“textBox2.AppendText = text”行。

6 个答案:

答案 0 :(得分:26)

使用以下

textBox2.AppendText(text);

而不是

textBox2.AppendText = text;

AppendText不是属性,而是方法。因此需要使用参数调用它,不能直接分配。

属性是特殊方法,由于编译器中的特殊处理而支持赋值。

答案 1 :(得分:5)

改为执行此操作(AppendText是一个方法,而不是属性;这正是错误消息告诉您的内容):

textBox2.AppendText(text);

答案 2 :(得分:5)

textBox2.AppendText(text);method。你必须把它称为一个。您正在对方法执行赋值操作。

答案 3 :(得分:5)

您必须以这种方式调用AppendText:

textBox1.AppendText("Some text")

答案 4 :(得分:5)

AppendText是一种方法,你必须调用它。

textBox2.AppendText(text);

答案 5 :(得分:0)

我发现声明的变量名类似于方法名,因此不允许我赋值。
我更改名称的那一刻起就起作用了!