C#函数返回值错误 [英] C# Function return value error

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

问题描述

我在Windows窗体应用程序中有一个方法,我将其声明为:



I have a method in a Windows Form application that I''m making declared as:

public  Color get_color(double str)
  {
      Color clr;
     // int k = 0;
      if (Convert.ToDouble(str) < 25 && Convert.ToDouble(str) > 0)
      {
          // for (int j = 5; j < 11; j++)
          {
              return clr = Color.FromArgb(255, 0, 0);

          }


      }
  }





构建解决方案时我遇到错误:

错误3menu.Form2.get_color(double)'':并非所有代码路径都返回值



有人可以解释一下我在这里做错了什么,这真的很有帮助。



While building the solution I''m having the error:
Error 3 "menu.Form2.get_color(double)'': not all code paths return a value"

can someone please explain what is it that im doing wrong here, It''ll be really helpful.

推荐答案

如果条件,则需要从中写出 return 代码。

尝试以下代码:

You need to write return code out of if condition.
Try below code:
public  Color get_color(double str)
        {
            Color clr = new Color();
           // int k = 0;
            if (Convert.ToDouble(str) < 25 && Convert.ToDouble(str) > 0)
            {
                // for (int j = 5; j < 11; j++)
                {
                     clr = Color.FromArgb(255, 0, 0);
 
                }

            }
            return clr;
        }


在您编写的代码中,您已从中返回,如果条件。



但是如果执行失败了if条件怎么办?在这种情况下,没有返回值。

因此构建失败。



尝试为<$ c $设置返回条件c> else
部分。
In the code you have written, you have returned from within the if condition.

But what if execution fails the if condition. In that case there is no return value.
As a result build fails.

Try putting a return condition for the else part as well.


添加返回clr;在你结束你的方法之前。所以在任何情况下它都会返回一些东西。
Add return clr; before you end you method. so that at every case it will return something.


这篇关于C#函数返回值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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