不可调用的成员不能像方法一样使用?

Ang*_*wzz 35 c#

我的程序中出现以下错误:

'System.Windows.Forms.TextBox.Text' is a 'property' but used like a 'method'
Run Code Online (Sandbox Code Playgroud)

Non-invocable member 'System.Windows.Forms.Control.Text' cannot be used like a method.
Run Code Online (Sandbox Code Playgroud)

这是代码:

if (OffenceBox.Text != "")
   {
 AddBook(int.Parse(AgeBox.Text), NameBox.Text, AddressBox.Text, (HeightBox.Text), OffenceBox.Text());
   }
   else
   {
   MessageBox.Show("Age must be max 3 numbers in length");
   }
   }
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

编辑:修复错误,现在遇到另一个: Argument 4: Cannot convert String to int我似乎无法解决问题.

pat*_*ter 72

在你写"OffenceBox.Text()"的地方,你需要用"OffenceBox.Text"替换它.这是一个属性,而不是一个方法 - 错误中的线索!


Dan*_*ira 7

之所以发生这种情况,是因为您尝试使用属性"OffenceBox.Text"就像一个方法.尝试从中删除parenteses OffenceBox.Text(),它会工作正常.

请记住,您无法在类中创建具有相同名称的方法和属性.


顺便说一句,一些别名可能会让你感到困惑,因为有时它是方法或属性,例如:"Count"别名:


命名空间:System.Linq

using System.Linq

namespace Teste
{
    public class TestLinq
    {
        public return Foo()
        {
            var listX = new List<int>();
            return listX.Count(x => x.Id == 1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


命名空间:System.Collections.Generic

using System.Collections.Generic

namespace Teste
{
    public class TestList
    {
        public int Foo()
        {
            var listX = new List<int>();
            return listX.Count;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gab*_*abe 5

如果您得到了这个答案,那是因为上述答案均不适用于您的情况。毕竟,您正在尝试实例化一个类,而不是使用一个属性。问题是您忘记或意外删除了“new”关键字。

var obj = YourClass(); // Will throw the non-invocable error.
var obj = new YourClass(); // shhh, it's our secret. We can pretend this never happened.
Run Code Online (Sandbox Code Playgroud)