22

Getting the error

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.

5
  • 1
    Thanks guys. Sorry if I was such an idiot :(
    – puretppc
    Commented Nov 4, 2013 at 16:42
  • Uh I tried it and it worked but for some reason, it won't display it in a new line.
    – puretppc
    Commented Nov 4, 2013 at 16:55
  • 2
    Does the textbox have MultiLine = True? Also if one of the people below does answer your question, please accept their answer by clicking the tick next to it
    – Basic
    Commented Nov 4, 2013 at 16:58
  • Yeah I set the properties to true but it still didn't work. :(
    – puretppc
    Commented Nov 4, 2013 at 17:00
  • 1
    Add a new line after the text to be appended: textBox2.AppendText(text + Environment.NewLine);
    – Idle_Mind
    Commented Nov 4, 2013 at 17:00

6 Answers 6

37

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.

6

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

textBox2.AppendText(text);
5

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

5

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);
1

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!

3
  • This seems to focused on your own environment and situation. Please rephrase to turn this into an asnwer which is more generally helpful to others.
    – Yunnosch
    Commented Dec 8, 2019 at 22:57
  • 1
    I disagree with the comment above. Just because the solution solves a rarer error than the absolute most common error that any programmer will ever encounter, that doesn't make it value-less. What would make SO value-less if the only questions allowed are those that are related to just a small handful of the most common problems people encounter. SO has much more value when you can find solutions to EVERY problem someone runs into, not just the few that you have in the front of your mind. Commented Sep 14, 2021 at 16:04
  • It certainly helped me! Thank you @prattek! Commented May 30, 2022 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.