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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18135758/
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 11:24:34  来源:igfitidea点击:

Non-invocable member cannot be used like a method?

c#

提问by Angelrawzz

I keep getting the following errors in my program:

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

'System.Windows.Forms.TextBox.Text' is a 'property' but used like a 'method'

and

Non-invocable member 'System.Windows.Forms.Control.Text' cannot be used like a method.

Here is the code:

这是代码:

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

How can I fix this problem?

我该如何解决这个问题?

EDIT: Fixed the error and now encountered another: Argument 4: Cannot convert String to intand I can't seem to fix the problem.

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

采纳答案by pattermeister

Where you've written "OffenceBox.Text()", you need to replace this with "OffenceBox.Text". It's a property, not a method - the clue's in the error!

在您编写“OffenceBox.Text()”的地方,您需要将其替换为“OffenceBox.Text”。这是一个属性,而不是一个方法 - 线索在错误中!

回答by SLaks

As the error clearly states, OffenceBox.Text()is not a function and therefore doesn't make sense.

正如错误明确指出的那样,OffenceBox.Text()不是函数,因此没有意义。

回答by Daniel Moreira

It have happened because you are trying to use the property "OffenceBox.Text" like a method. Try to remove parenteses from OffenceBox.Text()and it'll work fine.

发生这种情况是因为您试图像使用方法一样使用属性“OffenceBox.Text”。尝试从中删除括号OffenceBox.Text(),它会正常工作。

Remember that you cannot create a method and a property with the same name in a class.

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



By the way, some alias could confuse you, since sometimes it's method or property, e.g: "Count" alias:

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



Namespace: System.Linq

命名空间: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);
        }
    }
}



Namespace: System.Collections.Generic

命名空间:System.Collections.Generic

using System.Collections.Generic

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


回答by wendykcoding

I had the same issue and realized that removing the parentheses worked. Sometimes having someone else read your code can be useful if you have been the only one working on it for some time.

我遇到了同样的问题,并意识到删除括号有效。有时,如果您一直是唯一一个研究代码的人,那么让其他人阅读您的代码会很有用。

E.g.

例如

  cmd.CommandType = CommandType.Text(); 

Replace: cmd.CommandType = CommandType.Text;

替换: cmd.CommandType = CommandType.Text;