Non-invocable member cannot be used like a method?

前端 未结 4 1797
醉话见心
醉话见心 2020-12-16 08:42

I keep getting the following errors in my program:

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

and

相关标签:
4条回答
  • 2020-12-16 09:24

    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.

    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:


    Namespace: 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

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

    • Source - Linq: https://msdn.microsoft.com/library/bb338038(v=vs.100).aspx
    • Source - List: https://msdn.microsoft.com/pt-br/library/27b47ht3(v=vs.110).aspx
    0 讨论(0)
  • 2020-12-16 09:25

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

    0 讨论(0)
  • 2020-12-16 09:37

    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;

    0 讨论(0)
  • 2020-12-16 09:40

    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!

    0 讨论(0)
提交回复
热议问题