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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19772519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:50:48  来源:igfitidea点击:

Cannot Assign because it is a method group C#?

c#.netmethodsassignmethod-group

提问by puretppc

Cannot Assign "AppendText" because it is a "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.");
    }
}

The error is on the line "textBox2.AppendText = text", inside the button1_Click method.

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

采纳答案by Tilak

Use following

使用以下

textBox2.AppendText(text);

Instead of

代替

textBox2.AppendText = text;

AppendTextis not a property but a method. Thus it needs to be invoked with parameter and cannot be assigned directly.

AppendText不是属性而是方法。因此需要带参数调用,不能直接赋值。

Properties are special methods, that support assignments due to special handling in compiler.

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

回答by Mansfield

Do this instead (AppendText is a method, not a property; which is exactly what the error message is telling you):

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

textBox2.AppendText(text);

回答by P.Brian.Mackey

textBox2.AppendText(text);is a method. You have to call it like one. You were performing an assignment operation on a method.

textBox2.AppendText(text);是一种方法。你必须像一个一样称呼它。您正在对方法执行赋值操作。

回答by Stefano Bafaro

You have to call the AppendText in this way:

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

textBox1.AppendText("Some text")

回答by bbeda

AppendText is a method and you must call it.

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

textBox2.AppendText(text);

回答by prateek bhumkar

I figured out that the variable name declared was similar to a method name and hence it didn't allow me to assign a value.
The moment I changed the name it worked!

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