Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to write bool function as shown below


C#
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;
            }
        }



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
Posted
Comments
Richard MacCutchan 10-Feb-14 9:09am    
Your loop will only ever execute once, which means you ignore all potential true conditions further down your list. You should move the false return out of the loop and make it the default for when all items in the list have been tested but no item matches.

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.
C#
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;

        }
 
Share this answer
 
Comments
Member 10408451 10-Feb-14 9:18am    
Hi,
This code is fine. But I am getting warning saying

Unreachable code detected
Karthik_Mahalingam 10-Feb-14 9:26am    
no worries
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...
 
Share this answer
 
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900