运算符'=='不能应用于'方法组'类型的操作数 [英] Operator '==' cannot be applied to operand of type 'method group'

查看:53
本文介绍了运算符'=='不能应用于'方法组'类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数返回 true 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");
   }
}

我该如何解决?

推荐答案

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

正如科迪·格雷(Cody Gray)指出的,比较"true"和ValidateURL()( ValidateURL()== true )返回的值没有意义.没有人真正做到这一点,它只会使代码更长.当我回答问题时,我只是快速复制,粘贴并修复了OP的问题,这就是为什么要进行比较的原因.虽然绝对有效,但这并不是真正需要的.+1科迪.

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.

这篇关于运算符'=='不能应用于'方法组'类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆