编译错误:并非所有代码路径都返回值 [英] compile error:not all code paths return a value

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

问题描述

朋友们,

我的代码有什么问题,给我一个错误?

Hi friends,

What''s the problem of my code that gives me an error?

public int f()
{
    int x = 8;
        int y = 8;

    PictureBox[,] pic = new PictureBox[8, 8];
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            pic[i, j] = pictureBox34;
            Console.WriteLine(String.Format("[i,j] = [{0},{1},{2}]", i, j));
        }
    }
...
...

推荐答案

您的方法返回一个int

public int f()

因此,它必须返回一个值


Your method returns an int

public int f()

So, it has to return a value


public int f()
{
    int x = 8;
        int y = 8;
    PictureBox[,] pic = new PictureBox[8, 8];
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            pic[i, j] = pictureBox34;
            Console.WriteLine(String.Format("[i,j] = [{0},{1},{2}]", i, j));
        }
    }

    return x; // what do you want to return??
}



如果不需要返回值,请使用void

public void f()



If you don''t need a return value, use void

public void f()


阿鲁什,

您发布的函数还不完整,但是从编译错误中我可以看到您没有在某些代码路径中返回int.
这意味着在您的功能中,如果您有其他陈述,
在某些但不是所有分支中都有return语句.
并且确保在函数定义的右括号之前没有返回语句.

如果需要更多说明,请随时对此评论发表评论.

干杯,


曼弗雷德(Manfred)
Hi Arash,

the function you posted isn''t complete, but from your compilation error I can see that you''re not returning an int in some code paths.
This means that further down in your fucntion you have some if statments
and there are return statements in some but not all branches.
And for sure there is no return statment right before the closing brace of the function definition.

If I need to do more explaining, please feel free to post a comment to this answer.

Cheers,


Manfred


这篇关于编译错误:并非所有代码路径都返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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