Cannot Assign because it is a method group C#?

吃可爱长大的小学妹 提交于 2019-12-21 03:22:29

问题


Cannot Assign "AppendText" because it is a "method group".

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.


回答1:


Use following

textBox2.AppendText(text);

Instead of

textBox2.AppendText = text;

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

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




回答2:


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

textBox2.AppendText(text);



回答3:


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




回答4:


You have to call the AppendText in this way:

textBox1.AppendText("Some text")



回答5:


AppendText is a method and you must call it.

textBox2.AppendText(text);



回答6:


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!



来源:https://stackoverflow.com/questions/19772519/cannot-assign-because-it-is-a-method-group-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!