“并非所有代码路径都返回值"是什么意思? [英] what does it mean by "not all code paths returns a value"?

查看:386
本文介绍了“并非所有代码路径都返回值"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在col1_button_click(int count)上给出错误,并非所有代码路径都返回一个值"
这是什么意思?如何删除此错误?请问有人可以帮助我吗?

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?

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

}

推荐答案

计数为3时会发生什么?
在这种情况下,您的代码将不会返回任何内容-而这正是错误的含义.

使用return语句时,必须涵盖所有情况.
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.


这意味着:函数中的 至少有一条路径没有return语句.现在,由于您已声明它返回了某些内容,因此您是个骗子,而我不会编译它" .
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".


这意味着您需要在最后一个花括号之前的return语句.

顺便说一句,我会用这种方法做同样的事情:

It means you need a return statement just before the last curly brace.

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

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.


这篇关于“并非所有代码路径都返回值"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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