如何解决bool函数中的错误 [英] How to solve error in bool function

查看:143
本文介绍了如何解决bool函数中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试编写bool函数,如下所示





Hi,

I am trying to write bool function as shown below


public bool retunPGN(int m,CANBusDetails busOneDetails)
        {

            List<PGNsegmentTxMap> PGNsegmentMapList123 = new List<PGNsegmentTxMap>();

            PGNsegmentMapList123 = ReadDatabase();

            for (int i = 0; i < PGNsegmentMapList123.Count; i++)
            {
                if ((PGNsegmentMapList123[i].PGN == busOneDetails.CANMessageDetailsList[m].HexMessageId)
                    && (PGNsegmentMapList123[i].segmentTx == select1))

                    return true;
                else
                    return false;
            }
        }







错误:

'SCANLA.GatewayMsgIDs.retunPGN(int,SCANLA.Common.CANBusDetails)':并非所有代码路径都返回值



有谁能帮我解决这个问题? ?



谢谢

John




Error:
'SCANLA.GatewayMsgIDs.retunPGN(int, SCANLA.Common.CANBusDetails)': not all code paths return a value

Can anyone help me in solving this problem??

Thanks
John

推荐答案

试试这段代码..

它没有使用从循环值重新生成,因为它只返回集合中的第一个值。无论如何,这将帮助你解决bool返回问题。

Try this code..
Its no use of reurning from the loop values as it will return only the first value in the collection. anyhow this will help you from that bool return issue.
public bool retunPGN(int m, CANBusDetails busOneDetails)
        {
            bool flag = false;
            List<PGNsegmentTxMap> PGNsegmentMapList123 = new List<PGNsegmentTxMap>();

            PGNsegmentMapList123 = ReadDatabase();

            for (int i = 0; i < PGNsegmentMapList123.Count; i++)
            {
                if ((PGNsegmentMapList123[i].PGN == busOneDetails.CANMessageDetailsList[m].HexMessageId)
                    && (PGNsegmentMapList123[i].segmentTx == select1))
                    flag = true;
                break;

            }
            return flag;

        }


可能永远不会命中 - 列表中没有成员。

在这种情况下,有一个代码路径不返回 - 无论是真还是假...



简单来说,添加return(false)或return( true)刚刚关闭for ...
It is possible that for will never hit - no members in your list.
In that case there is a code-path that does not do return - either true or false...

In simple words, add return(false) or return(true) just after the closing of the for...


如果 PGNsegmentMapList123.Count 等于零,则没有<$ c将执行$ c> return 语句。通过在子程序结束时输入 return false; 来解决这个问题。



更重要的是, if 语句只会对 PGNsegmentMapList123 中的第一项执行,因为你正在执行 return if 语句是真还是假。
If PGNsegmentMapList123.Count is equal to zero, then no return statement will be executed. Resolve this by putting return false; at the end of the subroutine.

More importantly, the if statement will only be executed for the first item in PGNsegmentMapList123 since you are executing a return whether the if statement is true or false.


这篇关于如何解决bool函数中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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