Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
this code gives an error on col1_button_click(int count), "not all code paths returns a value"
what does it mean?how can i remove this error?pls can any one help me?

C#
public int returnVal1;

private void button1_Click(object sender, EventArgs e)
{
    returnVal1= col1_button_click(clickCountC1);
}

private int col1_button_click(int count)
{
    if (count == 0)
    {
        count = 1;
        return count;
    }
    else if (count == 1)
    {
        count = 2;
        return count;
    }
    else if (count ==2)
    {
        count = 0;
        return count;
    }

}
Posted
Updated 5-Jul-11 5:47am
v5
Comments
#realJSOP 5-Jul-11 11:49am    
Stop changing it back to the original version. All I did was put the pre block around it so it would show up as a block of code. This is a standard Codeproject style, and you would do well to learn about it if you intended to keep posting here.

It means: "in your function there is at least one path without a return statement. Now, since you have declared it returning something, you're a liar and I won't compile it".
 
Share this answer
 
Comments
Uday P.Singh 5-Jul-11 13:56pm    
good one my 5!
Sergey Alexandrovich Kryukov 5-Jul-11 14:17pm    
Best explanation so far, to my taste :-) A 5.
--SA
What happens when count is, say, 3?
In that case your code will not return anything - and that is exactly what the error means.

Every case must be covered while using a return statement.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 14:17pm    
Nice explanation, a 5.
--SA
Abhinav S 5-Jul-11 14:41pm    
Thank you.
Sergey Alexandrovich Kryukov 6-Jul-11 16:03pm    
I just fixed capitalization and added '?' in first line. Do you mind?
--SA
Abhinav S 7-Jul-11 3:23am    
No problem. Thanks for fixing the answer.
Sergey Alexandrovich Kryukov 8-Jul-11 1:33am    
My pleasure.
--SA
It means you need a return statement just before the last curly brace.

BTW, I'd do the same method this way:

C#
private int col1_button_click(int count)
{
    count++;
    if (count >= 2)
    {
        count = 0;
    }
    return count;
}


It's bad practice to have more than one exit point in a method. Whenever possible, you should avoid it.
 
Share this answer
 
v2
Comments
Peter Mulholland 5-Jul-11 11:54am    
++count;
as you're not using the value before the increment,
but I assume this gets optimized out anyway.
#realJSOP 5-Jul-11 13:03pm    
It doesn't matter, because it does get optimized out.
Peter Mulholland 5-Jul-11 16:34pm    
I wouldn't say it doesn't matter, using the correct option demonstrates an understanding of the difference between the 2, (not for a second suggesting you don't John) even tho we should never have to worry about the cost of the post increment any more. It's good practice to write what you mean and use the best tool for the job.
BobJanova 7-Jul-11 7:03am    
private int col1_button_click(int count){
return ++count % 3;
}

I don't actually agree that multiple return points are bad practice in all cases. (In particular 'guard' returns at the top of a method are clear and useful, and a switch where every case is a return or throw inside a simple method is not a problem.) But you are dead on with this example.
what is clickCountC1 in this line col1_button_click(clickCountC1);? I think you are passing wrong/insufficient value to col1_button_click(int count) method. where is clickCountC1 defined?

And do this:

C#
private int col1_button_click(int count)
{
if (count == 0)
{
   count = 1;
   return count;
}
else if (count == 1)
{
   count = 2;
   return count;
}
else if (count ==2)
{
   count = 0;
   return count;
}
return count;
}


hope this helps :)
 
Share this answer
 
v4

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