类型可转换为“布尔"的对象是必需的 [英] An object of a type convertible to 'bool' is required

查看:241
本文介绍了类型可转换为“布尔"的对象是必需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public bool  IsCellOrRowHeader(int x, int y)
       {
           try
           {
               DataGridViewHitTestType dgt = dgv.HitTest(x, y).Type;
               return (dgt == DataGridViewHitTestType.Cell ||
                          dgt == DataGridViewHitTestType.RowHeader);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Limit Viewer");
               writeErrorlog("Limit Viewer ( IsCellOrRowHeader)", ex.Message, ex.StackTrace);
               return;//An object of a type convertible to 'bool' is required
           }
       }

推荐答案

您的代码应如下所示.

Your code should be as following.

public bool IsCellOrRowHeader(int x, int y)
        {
            try
            {
                DataGridViewHitTestType dgt = dgv.HitTest(x, y).Type;
                return (dgt == DataGridViewHitTestType.Cell ||
                           dgt == DataGridViewHitTestType.RowHeader);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Limit Viewer");
                writeErrorlog("Limit Viewer ( IsCellOrRowHeader)", ex.Message, ex.StackTrace);
                return false;//
            }
        }





您的方法返回类型为bool,因此所有代码路径均应返回布尔值.在这种情况下,您的catch块将返回void.因此,将 return;更改为return false;





Your method return type is bool so all the code path should return a boolean value. In this case your catch block is returning void. So change the return; to return false;


ankitparekh56写道:
ankitparekh56 wrote:

公共布尔IsCellOrRowHeader(int x ,int y)

public bool IsCellOrRowHeader(int x, int y)



您的方法签名指出返回类型是布尔值.





Your method signature states that the return type is a Boolean value.



ankitparekh56写道:
ankitparekh56 wrote:

catch(异常例外)
{
MessageBox.Show(例如消息,限制查看器");
writeErrorlog("Limit Viewer(IsCellOrRowHeader)",ex.Message,例如StackTrace);
return; //需要一个可转换为"bool"类型的对象
//设置为return = false;
}

catch (Exception ex)
{
MessageBox.Show(ex.Message, "Limit Viewer");
writeErrorlog("Limit Viewer ( IsCellOrRowHeader)", ex.Message, ex.StackTrace);
return;//An object of a type convertible to ''bool'' is required
// set as return = false;
}



如果发生异常,则不会返回布尔值.只需将其设置为true或false.由于签名说明,它必须为布尔值,即使存在异常,也必须将返回类型设置为布尔值.



You are not returning a boolean value in case of exception. Just set it to true or false. Since the signature states, it has to be boolean, you have to set the return type as boolean even though if there is an exception.


这篇关于类型可转换为“布尔"的对象是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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