Non-Invocable Member Cannot Be Used Like a Method?
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...
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 int and I can't seem to fix the problem.
7 Answers
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!
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: (v=vs.100).aspx
- Source - List: (v=vs.110).aspx
As the error clearly states, OffenceBox.Text() is not a function and therefore doesn't make sense.
If you've reached this answer it's because none of the answers above apply to your situation. After all, you are trying to instantiate a class, not use a property. The problem is that you forgot or accidentally deleted the "new" keyword.
var obj = YourClass(); // Will throw the non-invocable error.
var obj = new YourClass(); // shhh, it's our secret. We can pretend this never happened.
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;
For people coming from Google, this error can also arise if you're invoking a generic method but have forgotten one or more of the closing triangular brackets. E.g:
// Example 1
var myVar = GetSomeData<AClass();
// Example 2
var myVar2 = GetSomeOtherData<BClass<CClass>();
This is easy to spot when you don't have many generics in your method but gets harder the more nesting you've got.
In my context, I faced this error while calling Count()
the solution was to import LINQ
using System.Linq;