Susan Moty
Susan Moty

Reputation: 135

Operator '==' cannot be applied to operand of type 'method group'

I have following function which returns true or false:

public bool ValidateURL()
{
   if (string.IsNullOrEmpty(txt22.Text) & string.IsNullOrEmpty(txt33.Text))
   {
      return false;
   }
   else 
   {
      return true;
   }
}

Now following code is on a button but I am getting "Operator cannot be applied" error:

private void btn33_Click(object sender, EventArgs e)
{
   if (ValidateURL==true)
   {
      MessageBox.Show("Enter data");
   }
}

How can i fix it?

Upvotes: 13

Views: 33508

Answers (6)

Piotr Justyna
Piotr Justyna

Reputation: 4996

private void btn33_Click(object sender, EventArgs e)
{
    if (ValidateURL())
    {
        MessageBox.Show("Enter data");
    }
}

EDIT:

As Cody Gray pointed out, there's no real point in comparing "true" and the value returned by ValidateURL() (ValidateURL() == true). Nobody really does it and it just makes the code longer. When I answered the question, I just quickly copied, pasted and fixed OP's problem and this is why the comparison was there. While absolutely valid, it's not really needed. +1 Cody.

Upvotes: 14

Katie Kilian
Katie Kilian

Reputation: 6993

A Google search of this error led me here. In my case, it was because I was referencing a new property in an ASP.NET MVC Razor page. The property had been added to my model, but I had forgotten to compile the project. The Razor compiler couldn't find the property, and was assuming I was trying to reference an extension method.

Once I compiled, the error cleared up.

Upvotes: 0

Ryan Bennett
Ryan Bennett

Reputation: 3432

You need parentheses. Should be ValidateURL() == true

Upvotes: 3

Mark Mooibroek
Mark Mooibroek

Reputation: 7706

Change

if (ValidateURL==true)

to

if (ValidateURL() ==true)

Upvotes: 5

Chris Wenham
Chris Wenham

Reputation: 24037

You want

if (ValidateURL() == true)

Upvotes: 1

BrandonZeider
BrandonZeider

Reputation: 8152

Change it to:

if (ValidateURL())

Upvotes: 4

Related Questions