因为它是方法组C#,所以无法分配? [英] Cannot Assign because it is a method group C#?

查看:797
本文介绍了因为它是方法组C#,所以无法分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于它是一个方法组",因此无法分配"AppendText".

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.");
    }
}

该错误位于button1_Click方法内的"textBox2.AppendText = text"行上.

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

推荐答案

使用以下内容

textBox2.AppendText(text);

代替

textBox2.AppendText = text;

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

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.

这篇关于因为它是方法组C#,所以无法分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆